简体   繁体   English

Firebase - onAuthStateChanged() 在登录和注册后触发

[英]Firebase - onAuthStateChanged() fires both after sign in and sign up

I need to be able to figure out why did the listener fire - was it an initial sign up or sign in?我需要能够弄清楚为什么听众会被解雇——是初次注册还是登录?

I tried using part of the user payload that comes with the callback.我尝试使用回调附带的部分用户有效负载。 I tried comparing user.metadata.createdAt and user.metadata.lastLoginAt and this usually works - if I sign up, then sign out, and then sign in - it's all ok.我尝试比较 user.metadata.createdAt 和 user.metadata.lastLoginAt 并且这通常有效 - 如果我注册,然后注销,然后登录 - 一切正常。 But it is not ok when I sign up and then simply refresh the page.但是当我注册然后简单地刷新页面时就不行了。

Is there a way to see some metadata about when was the last time the auth status was changed or simple - to figure out if onAuthStateChanged fired because of a sign up or sign in.有没有办法查看有关上次更改身份验证状态的时间或简单的一些元数据 - 以确定 onAuthStateChanged 是否因注册或登录而被触发。

Thanks!谢谢!

The onAuthStateChanged() fires when the auth state changes, so that is indeed both for sign-ups and sign-ins. onAuthStateChanged()在 auth state 更改时触发,因此这确实适用于注册和登录。

If you want to distinguish between the two, you can:如果要区分两者,可以:

  • Either hook the explicit then that you get back from the signIn... / createUser method.然后挂钩显式, then您从signIn... / createUser方法返回。
  • Or you can compare the creationTime and lastSignInTime in theuser.metadata .或者您可以比较user.metadata中的creationTimelastSignInTime If they're the same, the user was just created.如果它们相同,则表示刚刚创建了用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM