繁体   English   中英

在 firebase 中检查用户是否为新用户的最佳方法是什么 谷歌使用 flutter 登录

[英]What is the best way to check if the user is new in firebase google sign in with flutter

我想在 Flutter 中使用谷歌登录的新用户登录后添加一些额外的功能。所以我需要知道用户是否是新用户。 如果用户是新用户,那么我想添加一些用户信息。 否则导航到另一个页面。

那么如何检查用户是否是新用户?

User object有一个metadata属性,它有两个属性: creationTimelastSignInTime 当这些间隔不超过几毫秒时,用户才刚刚创建。 如果它们相距较远,则该用户是在之前的 session 中创建的。

FirebaseAuth 用户在 metadata 属性中包含此数据。 我使用了以下代码块。 如果需要,您可以更改秒数。

bool isNewUser(User user) {
  DateTime now = DateTime.now();
  DateTime cTime = user.metadata.creationTime;
  int longAgo = 15; // to check account creation under last 15 seconds
  return now.difference(cTime).inSeconds > longAgo;
}

暂无
暂无

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

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