-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathindex.js
137 lines (115 loc) · 3.84 KB
/
index.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// [SNIPPET_REGISTRY disabled]
// [SNIPPETS_SEPARATION enabled]
// ==========================================================================================
// Docs: Snippets in this file are "general purpose" and are used on more than one docs page
// ==========================================================================================
function makeGoogleCredential(googleUser) {
// [START auth_make_google_credential]
const { GoogleAuthProvider } = require("firebase/auth");
const credential = GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);
// [END auth_make_google_credential]
}
function makeFacebookCredential(response) {
// [START auth_make_facebook_credential]
const { FacebookAuthProvider } = require("firebase/auth");
const credential = FacebookAuthProvider.credential(
response.authResponse.accessToken);
// [END auth_make_facebook_credential]
}
function makeEmailCredential(email, password) {
// [START auth_make_email_credential]
const { EmailAuthProvider } = require("firebase/auth");
const credential = EmailAuthProvider.credential(email, password);
// [END auth_make_email_credential]
}
function signOut() {
// [START auth_sign_out]
const { getAuth, signOut } = require("firebase/auth");
const auth = getAuth();
signOut(auth).then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
// [END auth_sign_out]
}
function authStateListener() {
// [START auth_state_listener]
const { getAuth, onAuthStateChanged } = require("firebase/auth");
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/v8/firebase.User
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
// [END auth_state_listener]
}
function currentUser() {
// [START auth_current_user]
const { getAuth } = require("firebase/auth");
const auth = getAuth();
const user = auth.currentUser;
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/v8/firebase.User
// ...
} else {
// No user is signed in.
}
// [END auth_current_user]
}
function setLanguageCode() {
// [START auth_set_language_code]
const { getAuth } = require("firebase/auth");
const auth = getAuth();
auth.languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// auth.useDeviceLanguage();
// [END auth_set_language_code]
}
function authWithCredential(credential) {
// [START auth_signin_credential]
const { getAuth, signInWithCredential } = require("firebase/auth");
// Sign in with the credential from the user.
const auth = getAuth();
signInWithCredential(auth, credential)
.then((result) => {
// Signed in
// ...
})
.catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.customData.email;
// ...
});
// [END auth_signin_credential]
}
function signInRedirect(provider) {
// [START auth_signin_redirect]
const { getAuth, signInWithRedirect } = require("firebase/auth");
const auth = getAuth();
signInWithRedirect(auth, provider);
// [END auth_signin_redirect]
}
function initializeWithCustomDomain() {
// [START auth_init_custom_domain]
const { initializeApp } = require("firebase/app");
const firebaseConfig = {
apiKey: "...",
// By default, authDomain is '[YOUR_APP].firebaseapp.com'.
// You may replace it with a custom domain.
authDomain: '[YOUR_CUSTOM_DOMAIN]'
};
const firebaseApp = initializeApp(firebaseConfig);
// [END auth_init_custom_domain]
}