Pregunta

I am using $firebaseSimpleLogin to log into Firebase using email/password.

It is working rather well when I log in using email/password, I could see sessionkey being saved automatically as a cookie.

However, would like to remember the log in such that user only have to log in once. So I included {rememberMe: true} during auth.

How do I check if the session is still alive at the beginning of the page being loaded?

¿Fue útil?

Solución

From your question, I assume you're using Angular JS.

You can execute a run block on your main module, which is run everytime the page is loaded. I don't know much about Angularfire, this is the code I'm using on a hack day project to check auth and redirect to the login page if needed.

FirebaseRef is a wrapper that points to my Firebase instance.

This also makes sure that the currentUser object is available in all scopes.

var minib = angular.module('minib', ['ngRoute', 'firebase']);

minib.run(function($rootScope, $location, $firebaseSimpleLogin, firebaseRef) {
  $rootScope.auth = $firebaseSimpleLogin(firebaseRef());
  $rootScope.auth.$getCurrentUser().then(function(user) {
    if (user) {
      $rootScope.currentUser = user;
    } else {
      $location.path('/login');
    }
  });
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top