简体   繁体   中英

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

I want to add some extra features after new user login with a google login in Flutter.so I need to know if a user is new or not. if the user is new then I want to add some user information. other wise navigate to another page.

So how can I check If the User is new or not?

The User object has a metadata property, which has two properties: creationTime and lastSignInTime . When those are no more than a few milliseconds apart, the user was just created. If they're further apart, the user was created in a previous session.

FirebaseAuth User contains this data in metadata prop. I used the following chunk of code. You can change the number of seconds if you want.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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