$ firebaseSimpleLogin和会话无需重新登录 [英] $firebaseSimpleLogin and session without re-login

查看:116
本文介绍了$ firebaseSimpleLogin和会话无需重新登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$ firebaseSimpleLogin使用电子邮件/密码登录到Firebase。

使用电子邮件/密码登录时,它工作的很好,我可以看到sessionkey被自动保存为一个cookie。

但是,想要记住登录,使用户只需要登录一次。
所以我在auth中包含了{rememberMe:true}。

如何检查会话在加载页面的开头是否仍然存在?

解决方案

从你的问题,我假设你正在使用Angular JS。

您可以在主模块上执行 run 块,每次加载页面时都会运行该块。我对Angularfire知之甚少,这是我在黑客日项目中使用的代码,如果需要的话,检查auth并重定向到登录页面。



FirebaseRef 是一个指向我的Firebase实例的包装器。



这也确保 currentUser 对象在所有作用域中都可用。

  var minib = angular.module('minib',['ngRoute','firebase']); 
$ b minib.run($ root $,$ firebaseSimpleLogin,$ firebaseSimpleLogin,firebaseRef){$ b $ rootScope.auth = $ firebaseSimpleLogin(firebaseRef());
$ rootScope。 auth。$ getCurrentUser()。then(function(user){
if(user){
$ rootScope.currentUser = user;
} else {
$ location.path( '/ login');
}
});
});


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?

解决方案

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');
    }
  });
});

这篇关于$ firebaseSimpleLogin和会话无需重新登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆