-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.js
More file actions
91 lines (84 loc) · 2.95 KB
/
credentials.js
File metadata and controls
91 lines (84 loc) · 2.95 KB
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
// Initialize Firebase
var config = {
apiKey: "AIzaSyCfHp6dkivD_7EUQyiCn3ulSoJo5L_qoE8",
databaseURL: "https://brewconsole.firebaseio.com",
storageBucket: "brewconsole.appspot.com",
};
firebase.initializeApp(config);
/**
* initApp handles setting up the Firebase context and registering
* a callback for the auth status.
*/
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('enjoy').style.display = 'block';
// [END_EXCLUDE]
firebase.database().ref("/batches/PB0057").once('value').then(function(response) {
console.log("cred GET response", response);
});
} else {
// Let's try to get a Google auth token programmatically.
// [START_EXCLUDE]
document.getElementById('quickstart-button').textContent = 'Sign-in';
document.getElementById('enjoy').style.display = 'none';
// [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();
};