Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

javascript - 如何检测用户是否已经登录Firebase?(How do I detect if a user is already logged in Firebase?)

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

(我在我的JavaScript文件中使用firebase节点api进行Google登录。)

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

This works fine and the user is able to login with his Google credentials.

(这可以正常工作,并且用户可以使用其Google凭据登录。)

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?

(在提示弹出窗口之前,有什么方法可以检查是否已经有一个登录用户?)

  ask by Jophin Joseph translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(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.
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...