Skip to content

Instantly share code, notes, and snippets.

@markshust
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save markshust/94f21fcd22e97ad7b11a to your computer and use it in GitHub Desktop.

Select an option

Save markshust/94f21fcd22e97ad7b11a to your computer and use it in GitHub Desktop.
Check Firebase auth token expiry with AngularJS
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