刷新后如何让用户登录到Firebase应用程序? [英] How do I keep a user logged into a firebase app after refresh?

查看:129
本文介绍了刷新后如何让用户登录到Firebase应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以firebase和angular构建的应用程序,并且希望能够在刷新页面后保持用户登录。现在,我有一个登录屏幕,其中有两个基本的输入字段绑定到一个控制器

  this.email =; 
this.pass =;
this.emessage =;

this.loginUser = function(){
ref.authWithPassword({
email:this.email,
password:this.pass
},函数(error,authData){
if(error){
console.log(Login Failed!,error);
this.emessage = error.message;
$作用域$ apply $)$ b $ else $ {
dataStorage.uid = authData.uid;
$ location.path('/ projects');
$ scope。$ apply ();
}
} .bind(this));



$ b $ p
$ b

这样很好,而且很有效,但是当用户刷新页面他们被注销。有什么办法,当控制器加载,看看用户是否已经登录和自动重定向?感谢!

解决方案

您现在拥有的代码可以处理用户登录的情况。要处理用户已经登录的情况,您需要监视auth状态。从有关监测身份验证状态的Firebase文档

  //创建一个记录当前认证状态的回调
var ref = new Firebase(https://< YOUR -FIREBASE-APP> .firebaseio.com);
ref.onAuth(function(authData){
if(authData){
console.log(User+ authData.uid ++authData.provider));
} else {
console.log(User is logged out);
}
});

通常情况下,您只想显示 else中的登录按钮这个函数。


I have an application built in firebase and angular, and am wanting to be able to keep users logged in after refreshing the page. Right now I have a login screen with two basic input fields which bind to a controller

    this.email = "";
    this.pass = "";
    this.emessage = "";

    this.loginUser = function() {
        ref.authWithPassword({
            email: this.email,
            password: this.pass
        }, function(error, authData) {
            if (error) {
                console.log("Login Failed!", error);
                this.emessage = error.message;
                $scope.$apply();
            } else {
                dataStorage.uid = authData.uid;
                $location.path('/projects');
                $scope.$apply(); 
            }
        }.bind(this));
    }

This is all fine and dandy and it works, but when the user refreshes the page they are logged back out. Is there some way to, when the controller loads, see if the user is already logged in and auto-redirect? Thanks!

解决方案

The code you now have handles the case where the user logs on. To handle cases where the user has already logged, you monitor auth state. From the Firebase documentation on monitoring auth state:

// Create a callback which logs the current auth state
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("User " + authData.uid + " is logged in with " + authData.provider);
  } else {
    console.log("User is logged out");
  }
});

Typically you only want to show the log on button in the else of this function.

这篇关于刷新后如何让用户登录到Firebase应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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