如何检测用户是否已登录Firebase? [英] How do I detect if a user is already logged in Firebase?

查看:138
本文介绍了如何检测用户是否已登录Firebase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JavaScript文件中使用firebase节点api来登录Google。

I'm using the firebase node api in my javascript files for Google login.

firebase.initializeApp(config);
let provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);

这可以正常工作,用户可以用他的Google凭据登录。当用户再次访问该页面时,弹出窗口再次打开,但是由于他已经登录,弹出窗口关闭,而不需要用户的任何交互。有什么办法可以在提示弹出窗口之前检查是否已经有登录的用户吗?

This works fine and the user is able to login with his Google credentials. When the user visits the page again, the popup opens again but since he has already logged in, the popup closes without requiring any interaction from the user. Is there any way to check if there is already a logged in user before prompting the popup?

推荐答案

https://firebase.google.com/docs/auth/web/manage-users

您必须添加auth状态更改观察器。

You have to add an auth state change observer.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

这篇关于如何检测用户是否已登录Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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