I'm working off of the SBAdmin Dashboard template and attempting to add Firebase auth. I have a log in state with the LoginController. I also use the LoginController in a dashboard directive (Top right, profile icon with log in/out button).
angular.module('sbAdminApp')
.controller('LoginCtrl', ['$scope', '$rootScope', '$state', 'Auth', function($scope, $rootScope, $state, Auth) {
console.log('LoginCtrl');
var auth = Auth;
$rootScope.user = {};
$scope.toggleSignIn = function () {
console.log("toggleSignIn");
if (typeof $rootScope.user.displayName === 'undefined') {
//if(!currentAuth)
auth.$signInWithPopup("google")
.then(function(firebaseUser) {
console.log("Signed in as:", firebaseUser.uid);
$state.go('dashboard.home')
})
.catch(function(error) {
console.log("Authentication failed:", error);
});
}else{
auth.$signOut();
$rootScope.user = {};
$state.go('login');
}
} // end toggleSignIn
// any time auth state changes, add the user data to scope
auth.$onAuthStateChanged(function(user) {
console.log("onAuthStateChanged");
//console.log(user);
if (user) {
console.log('logged in');
// User is signed in.
$rootScope.user.displayName = user.displayName;
$rootScope.user.email = user.email;
$rootScope.user.emailVerified = user.emailVerified;
$rootScope.user.photoURL = user.photoURL;
$rootScope.user.isAnonymous = user.isAnonymous;
$rootScope.user.uid = user.uid;
$rootScope.user.refreshToken = user.refreshToken;
$rootScope.user.providerData = user.providerData;
console.log('Rootscope.user set');
//console.log($rootScope.user);
$state.go('dashboard.home');
}
else{
console.log('logged out');
$rootScope.user = {};
}
});
$rootScope.$on("$stateChangeError", function(event, toState, toParams, fromState, fromParams, error) {
// We can catch the error thrown when the $requireSignIn promise is rejected
// and redirect the user back to the home page
if (error === "AUTH_REQUIRED") {
$state.go("login");
}
});
Sorry if I'm missing something or not submitting the question in the right format I'm fresh to Angular and this is my first post here.