forked from firebase/quickstart-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.js
99 lines (94 loc) · 3.59 KB
/
credentials.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
// TODO(DEVELOPER): Change the values below using values from the initialization snippet: Firebase Console > Overview > Add Firebase to your web app.
// Initialize Firebase
var config = {
apiKey: '<YOUR_API_KEY>',
databaseURL: '<YOUR_DATABASE_URL>',
storageBucket: '<YOUR_STORAGE_BUCKET_NAME>'
};
firebase.initializeApp(config);
/**
* initApp handles setting up the Firebase context and registering
* callbacks for the auth status.
*
* The core initialization is in firebase.App - this is the glue class
* which stores configuration. We provide an app name here to allow
* distinguishing multiple app instances.
*
* This method also registers a listener with firebase.auth().onAuthStateChanged.
* This listener is called when the user is signed in or out, and that
* is where we update the UI.
*
* When signed in, we also authenticate to the Firebase Realtime Database.
*/
function initApp() {
// Listen for auth state changes.
// [START authstatelistener]
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var isAnonymous = user.isAnonymous;
var uid = user.uid;
var providerData = user.providerData;
// [START_EXCLUDE]
document.getElementById('quickstart-button').textContent = 'Sign out';
document.getElementById('quickstart-sign-in-status').textContent = 'Signed in';
document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, ' ');
// [END_EXCLUDE]
} else {
// Let's try to get a Google auth token programmatically.
// [START_EXCLUDE]
document.getElementById('quickstart-button').textContent = 'Sign-in with Google';
document.getElementById('quickstart-sign-in-status').textContent = 'Signed out';
document.getElementById('quickstart-account-details').textContent = 'null';
// [END_EXCLUDE]
}
document.getElementById('quickstart-button').disabled = false;
});
// [END authstatelistener]
document.getElementById('quickstart-button').addEventListener('click', startSignIn, false);
}
/**
* Start the auth flow and authorizes to Firebase.
* @param{boolean} interactive True if the OAuth flow should request with an interactive mode.
*/
function startAuth(interactive) {
// Request an OAuth token from the Chrome Identity API.
chrome.identity.getAuthToken({interactive: !!interactive}, function(token) {
if (chrome.runtime.lastError && !interactive) {
console.log('It was not possible to get a token programmatically.');
} else if(chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else if (token) {
// Authrorize Firebase with the OAuth Access Token.
var credential = firebase.auth.GoogleAuthProvider.credential(null, token);
firebase.auth().signInWithCredential(credential).catch(function(error) {
// The OAuth token might have been invalidated. Lets' remove it from cache.
if (error.code === 'auth/invalid-credential') {
chrome.identity.removeCachedAuthToken({token: token}, function() {
startAuth(interactive);
});
}
});
} else {
console.error('The OAuth Token was null');
}
});
}
/**
* Starts the sign-in process.
*/
function startSignIn() {
document.getElementById('quickstart-button').disabled = true;
if (firebase.auth().currentUser) {
firebase.auth().signOut();
} else {
startAuth(true);
}
}
window.onload = function() {
initApp();
};