참고

https://firebase.flutter.dev/docs/auth/social

 

Social Authentication | FlutterFire

Social authentication is a multi-step authentication flow, allowing you to sign a user into an account or link

firebase.flutter.dev

Google icon event

InkWell(
  onTap: () async {
    int? result = await UserController.to.googleLogin();
    if (result != 1) {
    	Get.to(Splash());
    }
  },
)

UserController

  final UserRepository _userRepository = UserRepository();
  final principal = UserModel().obs;

  Future<int?> googleLogin() async {
    Map<String, dynamic> result = await _userRepository.googleLogin();
    if (result.containsKey("success")) {
      principal.value = await result["success"];
      print(principal.value.uid);
      return 1;
    } else {
      print(await result["fail"].code);
      return -1;
    }
  }

UserRepository

  final UserProvider _userProvider = UserProvider();

  Future<Map<String, dynamic>> googleLogin() async {
    GoogleSignInAccount? googleUser = await _userProvider.googleLogin();
    GoogleSignInAuthentication googleAuth = await googleUser!.authentication;
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    UserCredential userCredential =
        await FirebaseAuth.instance.signInWithCredential(credential);
    try {
      UserModel _newUser = UserModel(
        uid: userCredential.user!.uid,
        email: userCredential.user!.email,
        name: userCredential.user!.displayName,
        photoUrl: "photoUrl",
      );
      await _userProvider.joinDetail(_newUser);

      return {"success": _newUser};
    } on FirebaseAuthException catch (e) {
      return {"fail": e};
    }
  }

UserProvider

  Future<GoogleSignInAccount?> googleLogin() async =>
      await GoogleSignIn().signIn();

provider에서 google 로그인 요청 -> repository에서 결과값인 UserCredential을 UserModel로 받기 -> UserController에서 view로 결과 넘겨주기

 

Git Commit

https://github.com/mugon-dev/flutter_firebase_blog/commit/73f7631f3ef7300b17bebe384a954f17af56e1ea

 

구글 로그인 구현 및 클라우드 유저 정보 저장 · mugon-dev/flutter_firebase_blog@73f7631

Permalink This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. Browse files 구글 로그인 구현 및 클라우드 유저 정보 저장 Loading branch information Showing 9 changed files with 1

github.com

 

728x90

+ Recent posts