Skip to content

Commit b1950d6

Browse files
committed
Phone Auth snippets (current)
1 parent d595de7 commit b1950d6

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

auth/phone-auth.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// These samples are intended for Web so this import would normally be
2+
// done in HTML however using modules here is more convenient for
3+
// ensuring sample correctness offline.
4+
import firebase from "firebase/app";
5+
import "firebase/auth";
6+
7+
// Mask the global 'window' for this snippet file
8+
const window = {
9+
recaptchaVerifier: undefined
10+
};
11+
12+
function setLanguageCode() {
13+
// [START auth_set_language_code]
14+
firebase.auth().languageCode = 'it';
15+
// To apply the default browser preference instead of explicitly setting it.
16+
// firebase.auth().useDeviceLanguage();
17+
// [END auth_set_language_code]
18+
}
19+
20+
function recaptchaVerifierInvisible() {
21+
function onSignInSubmit() {
22+
// TODO(you): Implement
23+
}
24+
25+
// [START auth_phone_recaptcha_verifier_invisible]
26+
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('sign-in-button', {
27+
'size': 'invisible',
28+
'callback': (response) => {
29+
// reCAPTCHA solved, allow signInWithPhoneNumber.
30+
onSignInSubmit();
31+
}
32+
});
33+
// [END auth_phone_recaptcha_verifier_invisible]
34+
}
35+
36+
function recaptchaVerifierVisible() {
37+
// [START auth_phone_recaptcha_verifier_visible]
38+
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
39+
'size': 'normal',
40+
'callback': (response) => {
41+
// reCAPTCHA solved, allow signInWithPhoneNumber.
42+
// ...
43+
},
44+
'expired-callback': () => {
45+
// Response expired. Ask user to solve reCAPTCHA again.
46+
// ...
47+
}
48+
});
49+
// [END auth_phone_recaptcha_verifier_visible]
50+
}
51+
52+
function recaptchaVerifierSimple() {
53+
// [START auth_phone_recaptcha_verifier_simple]
54+
window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier('recaptcha-container');
55+
// [END auth_phone_recaptcha_verifier_simple]
56+
}
57+
58+
function recaptchaRender() {
59+
/** @type {firebase.auth.RecaptchaVerifier} */
60+
const recaptchaVerifier = window.recaptchaVerifier;
61+
62+
// [START auth_phone_recaptcha_render]
63+
recaptchaVerifier.render().then((widgetId) => {
64+
window.recaptchaWidgetId = widgetId;
65+
});
66+
// [END auth_phone_recaptcha_render]
67+
}
68+
69+
function phoneSignIn() {
70+
function getPhoneNumberFromUserInput() {
71+
return "+15558675309";
72+
}
73+
74+
// [START auth_phone_signin]
75+
const phoneNumber = getPhoneNumberFromUserInput();
76+
const appVerifier = window.recaptchaVerifier;
77+
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
78+
.then((confirmationResult) => {
79+
// SMS sent. Prompt user to type the code from the message, then sign the
80+
// user in with confirmationResult.confirm(code).
81+
window.confirmationResult = confirmationResult;
82+
// ...
83+
}).catch((error) => {
84+
// Error; SMS not sent
85+
// ...
86+
});
87+
// [END auth_phone_signin]
88+
}
89+
90+
function verifyCode() {
91+
function getCodeFromUserInput() {
92+
return "1234";
93+
}
94+
95+
/** @type {firebase.auth.ConfirmationResult} */
96+
const confirmationResult = undefined;
97+
98+
// [START auth_phone_verify_code]
99+
const code = getCodeFromUserInput();
100+
confirmationResult.confirm(code).then((result) => {
101+
// User signed in successfully.
102+
const user = result.user;
103+
// ...
104+
}).catch((error) => {
105+
// User couldn't sign in (bad verification code?)
106+
// ...
107+
});
108+
// [END auth_phone_verify_code]
109+
}
110+
111+
function getRecaptchaResponse() {
112+
const recaptchaWidgetId = "...";
113+
const grecaptcha = {};
114+
115+
// [START auth_get_recaptcha_response]
116+
const recaptchaResponse = grecaptcha.getResponse(recaptchaWidgetId);
117+
// [END auth_get_recaptcha_response]
118+
}

0 commit comments

Comments
 (0)