サインイン
Amplify は、Amplify Auth などのバックエンドリソースと対話できるクライアントライブラリを提供しています。
signIn API の使用
Future<void> signInUser(String username, String password) async { try { final result = await Amplify.Auth.signIn( username: username, password: password, ); await _handleSignInResult(result); } on AuthException catch (e) { safePrint('Error signing in: ${e.message}'); }}設定とユーザーのサインアップ方法によっては、1 つ以上の確認が必要になる場合があります。Amplify.Auth.signIn から返された SignInResult を使用して、サインインの次のステップを確認してください。値が done の場合、ユーザーは正常にサインインしています。
Future<void> _handleSignInResult(SignInResult result) async { switch (result.nextStep.signInStep) { case AuthSignInStep.confirmSignInWithSmsMfaCode: final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!; _handleCodeDelivery(codeDeliveryDetails); break; case AuthSignInStep.confirmSignInWithNewPassword: safePrint('Enter a new password to continue signing in'); break; case AuthSignInStep.confirmSignInWithCustomChallenge: final parameters = result.nextStep.additionalInfo; final prompt = parameters['prompt']!; safePrint(prompt); break; case AuthSignInStep.resetPassword: final resetResult = await Amplify.Auth.resetPassword( username: username, ); await _handleResetPasswordResult(resetResult); break; case AuthSignInStep.confirmSignUp: // Resend the sign up code to the registered device. final resendResult = await Amplify.Auth.resendSignUpCode( username: username, ); _handleCodeDelivery(resendResult.codeDeliveryDetails); break; case AuthSignInStep.done: safePrint('Sign in is complete'); break; }}
void _handleCodeDelivery(AuthCodeDeliveryDetails codeDeliveryDetails) { safePrint( 'A confirmation code has been sent to ${codeDeliveryDetails.destination}. ' 'Please check your ${codeDeliveryDetails.deliveryMedium.name} for the code.', );}signIn API のレスポンスには nextStep プロパティが含まれており、さらなるアクションが必要かどうかを判断するために使用できます。次のステップが返される可能性があります:
| 次のステップ | 説明 |
|---|---|
confirmSignInWithNewPassword | ユーザーは一時パスワードで作成されており、新しいパスワードを設定する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithCustomChallenge | サインインはカスタムチャレンジレスポンスで確認する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithTotpMfaCode | サインインはユーザーからの TOTP コードで確認する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithSmsMfaCode | サインインはユーザーからの SMS コードで確認する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithOtpCode | サインインはユーザーからのコード(SMS またはメールで送信)で確認する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithMfaSelection | ユーザーはサインイン前に MFA 確認のモードを選択する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithMfaSetupSelection | ユーザーはセットアップする MFA 確認のモードを選択する必要があります。confirmSignIn に "EMAIL" または "TOTP" を渡してプロセスを完了してください。 |
continueSignInWithTotpSetup | TOTP セットアッププロセスを続行する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithEmailMfaSetup | EMAIL セットアッププロセスを続行する必要があります。有効なメールアドレスを confirmSignIn に渡してプロセスを完了してください。 |
resetPassword | ユーザーは resetPassword を使用してパスワードをリセットする必要があります。 |
confirmSignUp | ユーザーはサインアップフローを完全に完了しておらず、confirmSignUp で確認する必要があります。 |
done | サインインプロセスが完了しました。 |
返される可能性がある MFA ステップの処理の詳細については、多要素認証を参照してください。
多要素認証が有効な場合
メールまたは SMS MFA が有効になっている場合、Cognito はユーザーに代わってメッセージを送信します。メールメッセージと SMS メッセージは、ユーザーがそれぞれメールアドレスと電話番号の属性を持っている必要があります。メール MFA または SMS MFA を使用する場合は、これらの属性をユーザープールで必須に設定することをお勧めします。これらの属性が必須の場合、ユーザーはサインアッププロセスを完了する前にこれらの情報を提供する必要があります。
MFA を必須に設定し、複数の認証要素を有効にした場合、Cognito は新しいユーザーに使用する MFA 要素を選択するよう促します。SMS を選択するには電話番号が必要で、メール MFA を選択するにはメールアドレスが必要です。
利用可能なメッセージベースの MFA に必要な属性が定義されていないユーザーがいる場合、Cognito は TOTP のセットアップを促します。
バックエンド認証リソースで MFA を有効にする方法の詳細については、多要素認証のドキュメントを参照してください。
サインインの確認
サインイン後、次のいずれかのタイプの nextStep がサインイン結果として返されます。ユーザーのレスポンスを収集し、confirmSignIn API に渡してサインインフローを完了してください。
| 次のステップ | 説明 |
|---|---|
confirmSignInWithTotpMfaCode | サインインはユーザーからの TOTP コードで確認する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithSmsMfaCode | サインインはユーザーからの SMS コードで確認する必要があります。confirmSignIn でプロセスを完了してください。 |
confirmSignInWithOtpCode | サインインはユーザーからのコード(SMS またはメールで送信)で確認する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithMfaSelection | ユーザーはサインイン前に MFA 確認のモードを選択する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithMfaSetupSelection | ユーザーはセットアップする MFA 確認のモードを選択する必要があります。confirmSignIn に MfaType.email.confirmationValue または MfaType.totp.confirmationValue を渡してプロセスを完了してください。 |
continueSignInWithTotpSetup | TOTP セットアッププロセスを続行する必要があります。confirmSignIn でプロセスを完了してください。 |
continueSignInWithEmailMfaSetup | EMAIL セットアッププロセスを続行する必要があります。有効なメールアドレスを confirmSignIn に渡してプロセスを完了してください。 |
外部 ID プロバイダーでサインイン
Google などの外部 ID プロバイダーを使用してサインインするには、signInWithWebUI 関数を使用します。
仕組み
Web UI でのサインインは、ウェブビュー内にサインイン UI を表示します。サインインプロセスが完了すると、サインイン UI はアプリにリダイレクトします。
プラットフォームのセットアップ
Web
Flutter Web アプリケーションでローカルに Hosted UI を使用するには、リダイレクト URI を設定したときに localhost に割り当てたポートの値を使用して、--web-port=3000 引数でアプリを実行する必要があります。
Android
アプリの android/app/src/main ディレクトリにある AndroidManifest.xml ファイルに以下の queries 要素を追加し、同じファイルの MainActivity に以下の intent-filter を追加してください。
必要に応じて myapp をリダイレクト URI スキームに置き換えてください:
<queries> <intent> <action android:name= "android.support.customtabs.action.CustomTabsService" /> </intent></queries><application> ... <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="myapp" /> </intent-filter> </activity> ...</application>macOS
XCode を開き、App Sandbox 機能を有効にして、「Network」の下の「Incoming Connections (Server)」を選択してください。
iOS、Windows、Linux
特定のプラットフォーム設定は必要ありません。
ソーシャル Web UI サインインの起動
これで、外部プロバイダーの Web UI でサインインを起動する準備ができました。
Future<void> socialSignIn() async { try { final result = await Amplify.Auth.signInWithWebUI( provider: AuthProvider.google, ); safePrint('Sign in result: $result'); } on AuthException catch (e) { safePrint('Error signing in: ${e.message}'); }}