在flutter中使用Firebase与Google登录时,如何检查用户是新用户还是现有用户? [英] How to check If user is new or existing when login with google using firebase in flutter?

查看:122
本文介绍了在flutter中使用Firebase与Google登录时,如何检查用户是新用户还是现有用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,在该项目中,用户数据将保存在Fire store中,当用户使用google登录时,我正在为用户制作新文档.这是我的下面的代码

I am working on a project in which user data will be saved in Fire store in this i am making new document for user when the user sign In with google. here is my code below

import 'package:firebase_auth/firebase_auth.dart';
import 'package:gfd_official/User/User.dart';
import 'package:gfd_official/services/database.dart';
import 'package:google_sign_in/google_sign_in.dart';

class GSignInhelp {
 final FirebaseAuth _auth = FirebaseAuth.instance;

 //Firebase User
 Userdat _userFromFirebase(User user) {
 return user != null ? Userdat(uid: user.uid) : null;
 }

 //auth change user stream
 Stream<Userdat> get user {
 return _auth.authStateChanges().map(_userFromFirebase);
}

Future signInWithGoogle() async {
GoogleSignIn googleSignIn = GoogleSignIn();
final account = await googleSignIn.signIn();
final auth = await account.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: auth.accessToken,
idToken: auth.idToken,
);
try {
final res = await _auth.signInWithCredential(credential);
User user = res.user;
await DatabaseService(uid: user.uid)
    .updateUserRole(user.displayName, user.email, 'basic');
return _userFromFirebase(user);
} catch (e) {
  print(e.toString());
  return null;
  }
}

Future logOut() async {
try {
  GoogleSignIn().signOut();
  return await _auth.signOut();
} catch (e) {
  print(e.toString());
  return null;
}
}
}

我能够成功更改火灾存储中的用户角色,但是如您所见,当用户登录数据库时,值将设置为默认值,但我不希望这样做.因此,我的问题是如何检查用户是首次登录还是返回用户.

I am successfully able to change user role in fire store but as you can see when user signs in the database values will be set to default but I don't want this. So, my question is how can I check that if user is signed in for first time or returning user.

如果用户要返回,那么我不想在Fire store中重置用户数据,如果用户是新用户,请使用默认值在Fire Store中创建文档.

If user is returning then I don't want to reset user data in Fire store and If user is new then create document in Fire store with default values.

结论,当用户使用既是现有用户又是新用户的google登录时,如何检查?

Conclusion, How can I check that When user signs in with google that is user a existing user or new a user?

推荐答案

您可以通过User Credential检查该用户是新用户还是先前注册的用户,如果还不是该用户,则可以检索authResult.additionalUserInfo.isNewUser之前注册过.

You can check the user whether he is a new or previously registered user via User Credential and through you can retrieve the authResult.additionalUserInfo.isNewUser if the user is not already registered before.

您可以使用以下代码:

 Future<UserCredential> signInWithGoogle(BuildContext context) async {

    FirebaseAuth _auth = FirebaseAuth.instance;
    // Trigger the authentication flow
    final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();

    // Obtain the auth details from the request
    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;

    // Create a new credential
    final GoogleAuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    final UserCredential authResult =
        await _auth.signInWithCredential(credential);

    final User user = authResult.user;

    ///Her to check isNewUser OR Not
    if (authResult.additionalUserInfo.isNewUser) {
      if (user != null) {
      //You can her set data user in Fire store
      //Ex: Go to RegisterPage()
        }

      }
    } else {
     
      //Ex: Go to HomePage()
    }

    return await FirebaseAuth.instance.signInWithCredential(credential);
  }

这篇关于在flutter中使用Firebase与Google登录时,如何检查用户是新用户还是现有用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆