I've used firebase simpleLogin with facebook in AngularJS. (Generated with angular and angularfire generators in yeoman)
How do I keep the user in a variable, $scope, $rootScope etc. correctly? The user is going to be used in another angular module.
The code looks like this:
'use strict';
angular.module('angularfire.login', ['firebase', 'angularfire.firebase'])
.run(function(simpleLogin) {
simpleLogin.init();
})
.factory('simpleLogin', function($rootScope, $firebaseSimpleLogin, firebaseRef, $timeout) {
function assertAuth() {
if( auth === null ) { throw new Error('Must call loginService.init() before using its methods'); }
}
var auth = null;
return {
init: function() {
auth = $firebaseSimpleLogin(firebaseRef());
return auth;
},
logout: function() {
assertAuth();
auth.$logout();
},
/**
* @param {string} provider
* @param {Function} [callback]
* @returns {*}
*/
login: function(provider, callback) {
assertAuth();
auth.$login(provider, {rememberMe: true}).then(function(user) {
if( callback ) {
//todo-bug https://github.com/firebase/angularFire/issues/199
$timeout(function() {
callback(null, user);
});
}
}, callback);
}
};
});
For instance $rootScope.user = user; is working, but I've been told that's not a clean method. Also, if I'm refreshing I'm stilled logged in, but the $rootscope.user becomes undefined.
Thanks