I'd like to have the copy-paste "Firebase Authorize using Google" flow for my app -- and I do, with one problem: it always redirects to Google, even when it is already authorized and bounces right back. Is there any way to have it not bounce through Google when the user has recently authenticated and is a returning user?
Promise.resolve().then(function(){
return new Promise(function(resolve) {
console.group('init');
firebase.initializeApp({
apiKey: "MY KEY",
authDomain: "MINE.firebaseapp.com",
databaseURL: "https://MINE.firebaseio.com",
storageBucket: "MINE.appspot.com",
messagingSenderId: "ALSOMINE"
});
// Using this to make sure not in intermediate state
firebase.auth().onAuthStateChanged(function(user) {
console.log('User state stablilized.');
resolve();
});
});
}).then(function() {
// All the various login states
return new Promise(function(resolve, reject) {
if (firebase.auth().currentUser) {
console.log('You were already logged in - great!')
resolve(firebase.auth().currentUser);
}
firebase.auth().getRedirectResult().then(function(result) {
if (result.user) {
console.log('Welcome back and congratulations on your successful login!', result.user);
resolve(result.user);
} else {
console.log('Not logged in, not returning - time to send you on your way.');
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/plus.login');
firebase.auth().signInWithRedirect(provider);
console.log('You should never see this line because it redirected!');
}
}).catch(function(error) {
console.error('Returning from redirect login error:', error);
reject(error);
});
});
}).then(function(user) {
console.log('Signed in user', firebase.auth().currentUser.email);
// LETS ROCK