7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter firebase_auth 0.18.0以降におけるログイン処理の実装例

Posted at

はじめに

本記事はFlutterでFirebase Authenticationを使ってGoogleアカウントログインを行うための情報を記載しています。
とても参考になる既存の記事は存在しますがfirebase_auth 0.18.0以前の内容がほとんどで、一部実装を書き換える必要があります。

参考にさせて頂いた記事

FlutterでFirebase Authenticationを使ったGoogleアカウントログインを実装してみた

pub.devのchangelog

該当のアップデート内容は主に以下の部分になります。

0.18.0
 BREAKING: The FirebaseUser class has been renamed to User.
 BREAKING: The AuthResult class has been renamed to UserCredential.
 DEPRECATED: All sub-class (e.g. GoogleAuthProvider) getCredential() methods have been deprecated in favor of credential().

実装例

main.dart
Future<User> _handleGoogleSignIn() async {
    final GoogleSignIn _googleSignIn = GoogleSignIn();
    final GoogleSignInAccount googleUser = (await _googleSignIn.signIn())!;
    final GoogleSignInAuthentication googleAuth = await googleUser.authentication;

    // getCredential()をcredential()に置き換える
    final AuthCredential credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    // AuthResultをUserCredentialに置き換える
    final UserCredential userCredential = await _auth.signInWithCredential(credential);

    // FirebaseUserをUserに置き換える。
    final User user = userCredential.user!;
    assert(user.email != null);
    assert(user.displayName != null);
    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);

    // FirebaseUserをUserに置き換える。
    final User currentUser = _auth.currentUser!;
    assert(user.uid == currentUser.uid);

    return user;
  }

まとめ

今回はGoogleアカウントによるログイン処理を例に上げましたが、Facebookやその他ソーシャルログインでも同様の対応が必要になります。

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?