javascript - How to check angularfire authentication status in app.js -
i following angularfire tutorial authenticate users. users able register, signin , signout in app.
when try use resolve check $requiresignin() routes(ui-route), doesn't work.
also, want check user authentication status($onauthstatechanged()) in app.js modules loaded. if there change in authentication, update authentication service( userauth). using ng-show="authdata" / ng-hide="authdata change html logged in elements. after login, of these elements still showing not logged in. think has authentication update status.
if user logged in , try visit login/signup page send them home page.
here app.js
angular .module('myapp', [ 'nganimate', 'ngaria', 'ngcookies', 'ngresource', 'ui.router', 'ngmeta', 'firebase' ]) .run(["$rootscope", "$state", function($rootscope, $state) { $rootscope.$on("$statechangeerror", function(event, tostate, toparams, fromstate, fromparams, error) { if (error === "auth_required") { $state.go("home"); } }); }]) .config(['$stateprovider', '$urlrouterprovider', '$locationprovider', function( $stateprovider, $urlrouterprovider, $locationprovider, ngmetaprovider, $location, userauth) { $urlrouterprovider.otherwise('/'); $stateprovider .state('/', { url: '/', templateurl: 'views/main.html', controller: 'mainctrl', meta: { 'title': 'goaf | next great destination', 'description': 'this description shown in google search results' } }) .state('home', { url: '/home', templateurl: 'views/home.html', controller: 'homectrl', meta: { 'title': 'goaf | next great destination', 'description': 'this description shown in google search results' }, resolve: { "currentauth": ["userauth", function(userauth) { return userauth.$requiresignin(); }] } }) }]);
here controller home page want logged in users:
angular.module('myapp') .controller('homectrl', ["currentauth", function ($rootscope, $scope, $http, $interval, $location, userauth, dataservice, currentauth) { $scope.authdata = userauth.isloggedin(); }]);
and here userauth service file:
angular.module('myapp') .service('userauth', ["$firebaseauth", function($firebaseauth) { var firebaseauthobject = $firebaseauth(); this.login = function(loginemail, loginpassword){ return firebaseauthobject.$signinwithemailandpassword(loginemail, loginpassword); }; this.isloggedin = function(){ return firebaseauthobject.$getauth(); }; this.signoutuser = function() { firebaseauthobject.$signout(); console.log('logging out user!'); }; } ]);
Comments
Post a Comment