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

查看:29
本文介绍了如何检测用户是否已登录 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

您必须添加一个身份验证状态更改观察者.

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天全站免登陆