-
Notifications
You must be signed in to change notification settings - Fork 248
/
index.js
114 lines (102 loc) · 3.32 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
// 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";
// ==========================================================================================
// 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]
var credential = firebase.auth.GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);
// [END auth_make_google_credential]
}
function makeFacebookCredential(response) {
// [START auth_make_facebook_credential]
var credential = firebase.auth.FacebookAuthProvider.credential(
response.authResponse.accessToken);
// [END auth_make_facebook_credential]
}
function makeEmailCredential(email, password) {
// [START auth_make_email_credential]
var credential = firebase.auth.EmailAuthProvider.credential(email, password);
// [END auth_make_email_credential]
}
function signOut() {
// [START auth_sign_out]
firebase.auth().signOut().then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});
// [END auth_sign_out]
}
function authStateListener() {
// [START auth_state_listener]
firebase.auth().onAuthStateChanged((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
var uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
// [END auth_state_listener]
}
function currentUser() {
// [START auth_current_user]
const user = firebase.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]
firebase.auth().languageCode = 'it';
// To apply the default browser preference instead of explicitly setting it.
// firebase.auth().useDeviceLanguage();
// [END auth_set_language_code]
}
function authWithCredential(credential) {
// [START auth_signin_credential]
// Sign in with the credential from the user.
firebase.auth()
.signInWithCredential(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.email;
// ...
});
// [END auth_signin_credential]
}
function signInRedirect(provider) {
// [START auth_signin_redirect]
firebase.auth().signInWithRedirect(provider);
// [END auth_signin_redirect]
}
function initializeWithCustomDomain() {
// [START auth_init_custom_domain]
firebase.initializeApp({
apiKey: '...',
// By default, authDomain is '[YOUR_APP].firebaseapp.com'.
// You may replace it with a custom domain.
authDomain: '[YOUR_CUSTOM_DOMAIN]'
});
// [END auth_init_custom_domain]
}