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

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

问题描述

我有一个内置 firebase 和 angular 的应用程序,我希望能够在刷新页面后让用户保持登录状态.现在我有一个登录屏幕,其中包含两个绑定到控制器的基本输入字段

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!

推荐答案

您现在拥有的代码处理用户登录的情况.要处理用户已经登录的情况,您可以监控身份验证状态.来自 Firebase 文档监控身份验证状态:

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

通常,您只想在此函数的 else 中显示登录按钮.

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

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

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