-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathphone-auth.js
124 lines (103 loc) · 3.39 KB
/
phone-auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
// Mask the global 'window' for this snippet file
const window = {
recaptchaVerifier: undefined
};
function recaptchaVerifierInvisible() {
function onSignInSubmit() {
// TODO(you): Implement
}
// [START auth_phone_recaptcha_verifier_invisible]
const { getAuth, RecaptchaVerifier } = require("firebase/auth");
const auth = getAuth();
window.recaptchaVerifier = new RecaptchaVerifier(auth, 'sign-in-button', {
'size': 'invisible',
'callback': (response) => {
// reCAPTCHA solved, allow signInWithPhoneNumber.
onSignInSubmit();
}
});
// [END auth_phone_recaptcha_verifier_invisible]
}
function recaptchaVerifierVisible() {
// [START auth_phone_recaptcha_verifier_visible]
const { getAuth, RecaptchaVerifier } = require("firebase/auth");
const auth = getAuth();
window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', {
'size': 'normal',
'callback': (response) => {
// reCAPTCHA solved, allow signInWithPhoneNumber.
// ...
},
'expired-callback': () => {
// Response expired. Ask user to solve reCAPTCHA again.
// ...
}
});
// [END auth_phone_recaptcha_verifier_visible]
}
function recaptchaVerifierSimple() {
// [START auth_phone_recaptcha_verifier_simple]
const { getAuth, RecaptchaVerifier } = require("firebase/auth");
const auth = getAuth();
window.recaptchaVerifier = new RecaptchaVerifier(auth, 'recaptcha-container', {});
// [END auth_phone_recaptcha_verifier_simple]
}
function recaptchaRender() {
const { RecaptchaVerifier } = require("firebase/auth");
/** @type {RecaptchaVerifier} */
const recaptchaVerifier = window.recaptchaVerifier;
// [START auth_phone_recaptcha_render]
recaptchaVerifier.render().then((widgetId) => {
window.recaptchaWidgetId = widgetId;
});
// [END auth_phone_recaptcha_render]
}
function phoneSignIn() {
function getPhoneNumberFromUserInput() {
return "+15558675309";
}
// [START auth_phone_signin]
const { getAuth, signInWithPhoneNumber } = require("firebase/auth");
const phoneNumber = getPhoneNumberFromUserInput();
const appVerifier = window.recaptchaVerifier;
const auth = getAuth();
signInWithPhoneNumber(auth, phoneNumber, appVerifier)
.then((confirmationResult) => {
// SMS sent. Prompt user to type the code from the message, then sign the
// user in with confirmationResult.confirm(code).
window.confirmationResult = confirmationResult;
// ...
}).catch((error) => {
// Error; SMS not sent
// ...
});
// [END auth_phone_signin]
}
function verifyCode() {
function getCodeFromUserInput() {
return "1234";
}
// TODO(samstern): Import ConfirmationResult type
/** @type {*} */
const confirmationResult = undefined;
// [START auth_phone_verify_code]
const code = getCodeFromUserInput();
confirmationResult.confirm(code).then((result) => {
// User signed in successfully.
const user = result.user;
// ...
}).catch((error) => {
// User couldn't sign in (bad verification code?)
// ...
});
// [END auth_phone_verify_code]
}
function getRecaptchaResponse() {
const recaptchaWidgetId = "...";
const grecaptcha = {};
// [START auth_get_recaptcha_response]
const recaptchaResponse = grecaptcha.getResponse(recaptchaWidgetId);
// [END auth_get_recaptcha_response]
}