Skip to content

Commit

Permalink
Anonymous sign in made right with automatic refresh when booting if u…
Browse files Browse the repository at this point in the history
…ser was previously logged.
  • Loading branch information
jeandat committed Jul 1, 2016
1 parent dcfc086 commit 711a70d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions app/favourite/favourite.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,26 @@
function authenticate() {
return $q(function (resolve, reject) {
auth = $firebaseAuth();
auth.$signInAnonymously().then(authDidSucceeded).catch(reject);
var unlisten = auth.$onAuthStateChanged(function(user){
if(user) {
$log.info('User known. Reloading user info…');
user.reload()
.then(_.wrap(user, initFaves))
.catch(reject);
} else {
$log.info('No known user. Signing in…');
auth.$signInAnonymously()
.then(initFaves)
.catch(reject);
}
});
//////////
function authDidSucceeded(user) {
$log.info('Logged in as %s with provider %s', user.uid, user.providerId);
function initFaves(user){
unlisten();
$log.info('Logged in as %s (%s) with provider %s', user.email, user.uid, user.providerId);
$log.debug('Provider data:', user.providerData);
var userRef = firebase.database().ref().child('users').child(user.uid);
userRef.set({provider: user.providerId});
if(!userRef.child('provider').toString()) userRef.set({provider: user.providerId});
service.faves = faves = $firebaseArray(userRef.child('favourites'));
resolve();
}
Expand Down

0 comments on commit 711a70d

Please sign in to comment.