简体   繁体   English

谷歌登录后颤动添加用户数据

[英]flutter add user data after google sign in

i have a problem with save user data to firestore i using sign in with google auth so after i want to add user data to firestore so i can not do this can you help me thanks.我在将用户数据保存到 firestore 时遇到问题,我使用 google auth 登录,所以在我想将用户数据添加到 firestore 之后,我不能这样做,你能帮我吗,谢谢。

this is my auth code这是我的授权码

 
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

class GoogleSignProvider extends ChangeNotifier{
  final googleSignIn = GoogleSignIn();
  GoogleSignInAccount? _user;
  GoogleSignInAccount? get user => _user;
  final FirebaseAuth _auth = FirebaseAuth.instance;
  bool result = false;

  Future googleLogin()async {
    try {
      final googleUSer = await googleSignIn.signIn();
      if (googleUSer == null ) return;
      _user = googleUSer;
      final FirebaseFirestore _firestore = FirebaseFirestore.instance;
      final googleAuth = await googleUSer.authentication;
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
      );
      await FirebaseAuth.instance.signInWithCredential(credential);
    UserCredential userCredential = await _auth.signInWithCredential(credential);
    User? user = userCredential.user;
    if (user != null){
      if (userCredential.additionalUserInfo!.isNewUser) {
        await _firestore.collection('users').doc(user.uid).set(
            {
              'username': user.displayName,
              'uid': user.uid,
              'profilePhoto': user.photoURL,
            }
        );
      }}
    return result;
    } catch (e) {
      print(e.toString());
    }
    notifyListeners();
  }
  Future logout() async {
    await googleSignIn.disconnect();
    FirebaseAuth.instance.signOut();
  }
}

Hey were you able to save the user to firestore?嘿,你能把用户保存到 Firestore 吗? I have pasted the answer that worked for me.我粘贴了对我有用的答案。

final userGoogle = FirebaseAuth.instance.currentUser!;
await _firestore
          .collection('users')
          .doc(cred.user!.uid)
          .set({
uid: userGoogle.uid,
        name: userGoogle.displayName!,
        email: userGoogle.email!,
        photoURL: userGoogle.photoURL!,
});

I'm curious if this would mean that every time I login using the google account, the photo from the Google account would overwrite the photoURL, assuming it was updated from let's say the profile page.我很好奇这是否意味着每次我使用 google 帐户登录时,来自 google 帐户的照片都会覆盖 photoURL,假设它是从配置文件页面更新的。 Could help little insight into it.可以帮助一点洞察力。

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

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