Last active
August 29, 2015 14:11
-
-
Save markshust/94f21fcd22e97ad7b11a to your computer and use it in GitHub Desktop.
Check Firebase auth token expiry with AngularJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Run($cookies, $firebaseAuth, $rootScope, $state, FBURL) { | |
| var authRef = new Firebase(FBURL), | |
| clock = parseInt(getServerTime(authRef) / 1000), | |
| expires = 0; | |
| $rootScope.auth = $firebaseAuth(authRef); | |
| $rootScope.logout = function() { | |
| $rootScope.auth.$logout(); | |
| $rootScope.checkSession(); | |
| }; | |
| $rootScope.checkSession = function() { | |
| if ($cookies.authToken) { | |
| authRef.authWithCustomToken($cookies.authToken, function(error, authData) { | |
| if (error) { | |
| $state.transitionTo('auth'); | |
| } else if (authData) { | |
| expires = authData.expires; | |
| $state.transitionTo('home'); | |
| $rootScope.checkExpiry(clock, expires); | |
| } else { | |
| $state.transitionTo('auth'); | |
| } | |
| }); | |
| } else { | |
| authRef.unauth(); | |
| $state.transitionTo('auth'); | |
| } | |
| }; | |
| $rootScope.checkSession(); | |
| $rootScope.checkExpiry = function(clock, expires) { | |
| if (clock >= expires) { | |
| $state.transitionTo('auth'); | |
| } | |
| setTimeout(function() { | |
| $rootScope.checkExpiry(clock + 1, expires) | |
| }, 1000); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment