-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathphone-auth.js
110 lines (96 loc) · 3.15 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
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/auth";
// 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]
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('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]
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('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]
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
// [END auth_phone_recaptcha_verifier_simple]
}
function recaptchaRender() {
/** @type {firebase.auth.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 phoneNumber = getPhoneNumberFromUserInput();
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(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";
}
/** @type {firebase.auth.ConfirmationResult} */
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]
}