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

Time:11-29

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?

CodePudding user response:

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.

CodePudding user response:

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