flutter × firebaseでSign in with Appleを実装できない。

dart

1import 'package:firebase_auth/firebase_auth.dart';2import 'package:flutter/material.dart';3import 'package:flutter_signin_button/button_list.dart';4import 'package:flutter_signin_button/button_view.dart';5import 'package:sign_in_with_apple/sign_in_with_apple.dart';6import 'package:provider/provider.dart';7 8import 'package:keyboard_planner_front/providers/page.dart';9import 'package:keyboard_planner_front/page/weekly.dart';10 11class LoginTemplate extends StatefulWidget {12 const LoginTemplate({Key? key}) : super(key: key);13 14 15 State<LoginTemplate> createState() => _LoginTemplate();16}17 18class _LoginTemplate extends State<LoginTemplate> {19 // 公式のを参考に作ったユーザー登録の関数20 Future<UserCredential> signInWithApple() async {21 print('AppSignInを実行');22 23 final rawNonce = generateNonce();24 25 // 現在サインインしているAppleアカウントのクレデンシャルを要求する。26 final appleCredential = await SignInWithApple.getAppleIDCredential(27 scopes: [28 AppleIDAuthorizationScopes.email,29 AppleIDAuthorizationScopes.fullName,30 ],31 );32 print('クレデンシャル');33 print(appleCredential);34 // Apple から返されたクレデンシャルから `OAuthCredential` を作成します。35 final oauthCredential = OAuthProvider("apple.com").credential(36 idToken: appleCredential.identityToken,37 rawNonce: rawNonce,38 );39 print(appleCredential);40 // Firebaseでユーザーにサインインします。もし、先ほど生成したnonceが41 // が `appleCredential.identityToken` の nonce と一致しない場合、サインインに失敗します。42 return await FirebaseAuth.instance.signInWithCredential(oauthCredential);43 }44 45 // 上のとほぼ一緒。登録とログインができる。46 Future<UserCredential> appleSignIn() async {47 print('AppSignInを実行');48 // To prevent replay attacks with the credential returned from Apple, we49 // include a nonce in the credential request. When signing in with50 // Firebase, the nonce in the id token returned by Apple, is expected to51 // match the sha256 hash of `rawNonce`.52 final rawNonce = generateNonce();53 54 // Request credential for the currently signed in Apple account.55 final appleCredential = await SignInWithApple.getAppleIDCredential(56 scopes: [57 AppleIDAuthorizationScopes.email,58 AppleIDAuthorizationScopes.fullName,59 ],60 );61 print(appleCredential);62 // Create an `OAuthCredential` from the credential returned by Apple.63 final oauthCredential = OAuthProvider("apple.com").credential(64 idToken: appleCredential.identityToken,65 rawNonce: rawNonce,66 );67 // ここに画面遷移をするコードを書く!68 Provider.of<CurrentPage>(context, listen: false).setCurrentPage(Pages.week);69 Navigator.push(70 context,71 PageRouteBuilder(72 pageBuilder: (_, __, ___) => const WeeklyPage(),73 transitionDuration: const Duration(seconds: 0),74 ));75 print(appleCredential);76 // Sign in the user with Firebase. If the nonce we generated earlier does77 // not match the nonce in `appleCredential.identityToken`, sign in will fail.78 return await FirebaseAuth.instance.signInWithCredential(oauthCredential);79 }80 81 82 Widget build(BuildContext context) {83 return Scaffold(84 appBar: AppBar(85 title: const Text('AppleSignIn'),86 ),87 body: Center(88 child: Column(89 mainAxisAlignment: MainAxisAlignment.center,90 children: [91 const Text('新規登録用です。'),92 const SizedBox(height: 20),93 Container(94 width: 200,95 height: 30,96 child: SignInButton(97 Buttons.Apple,98 onPressed: () async {99 signInWithApple();100 },101 ),102 ),103 const SizedBox(height: 20),104 const Text('ログイン用です。'),105 const SizedBox(height: 20),106 Container(107 width: 200,108 height: 30,109 child: SignInButton(110 Buttons.Apple,111 onPressed: () async {112 appleSignIn();113 },114 ),115 ),116 ],117 ),118 ));119 }120}121

コメントを投稿

0 コメント