Need to configure your backend?See Build a Backend →
サインアップ
Amplifyは、Amplify Authなどのバックエンドリソースと対話できるクライアントライブラリを提供します。
まず、signUp() APIを使用してバックエンドに新しいユーザーを作成できます:
/// ユーザー名、パスワード、メールアドレスを使用してユーザーをサインアップします。/// 必須属性は、アプリの設定によって異なる場合があります。Future<void> signUpUser({ required String username, required String password, required String email, String? phoneNumber,}) async { try { final userAttributes = { AuthUserAttributeKey.email: email, if (phoneNumber != null) AuthUserAttributeKey.phoneNumber: phoneNumber, // additional attributes as needed }; final result = await Amplify.Auth.signUp( username: username, password: password, options: SignUpOptions( userAttributes: userAttributes, ), ); await _handleSignUpResult(result); } on AuthException catch (e) { safePrint('Error signing up user: ${e.message}'); }}Future<void> _handleSignUpResult(SignUpResult result) async { switch (result.nextStep.signUpStep) { case AuthSignUpStep.confirmSignUp: final codeDeliveryDetails = result.nextStep.codeDeliveryDetails!; _handleCodeDelivery(codeDeliveryDetails); break; case AuthSignUpStep.done: safePrint('Sign up 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.', );}signUp APIレスポンスには nextStep プロパティが含まれており、これを使用して追加のアクションが必要かどうかを判定できます。以下のネクストステップが返される可能性があります:
| ネクストステップ | 説明 |
|---|---|
confirmSignUp | サインアップをユーザーからコードを収集して confirmSignUp を呼び出すことで確認する必要があります。 |
done | サインアッププロセスが完全に完了しました。 |
サインアップの確認
デフォルトでは、サインアップしたすべてのユーザーは、メールアドレスまたは電話番号に送信された確認コードで確認するまで、未確認のステータスのままです。以下は、loginWith オプションとして phone または email が使用される場合に使用されるデフォルトの検証方法です。
| ログインオプション | ユーザーアカウント検証チャネル |
|---|---|
phone | 電話番号 |
email | メール |
email と phone | メール |
ユーザーから確認コードを受け取った後、サインアップを確認できます:
Future<void> confirmUser({ required String username, required String confirmationCode,}) async { try { final result = await Amplify.Auth.confirmSignUp( username: username, confirmationCode: confirmationCode, ); // Check if further confirmations are needed or if // the sign up is complete. await _handleSignUpResult(result); } on AuthException catch (e) { safePrint('Error confirming user: ${e.message}'); }}