2
1

More than 1 year has passed since last update.

AWS CognitoとUnityによるパスワードなしメール認証を試してみた。

Posted at

概要

CognitoとUnityによるパスワードなしメール認証について情報が少なく、ハマったので方法をシェアします。

準備

以下の記事を参考にAWSの設定を行ってください。
https://dev.classmethod.jp/articles/cognito-lambda-passwordless/#toc-10

Unityでのポイント

ポイントは以下の2点です。

  1. SignUpAsyncでユーザープールにユーザーを作成した後に、StartWithCustomAuthAsyncを叩いてCustomAuthを開始する必要がある。
  2. さらにメールで受け取ったコードを入力としてRespondToCustomAuthAsyncを叩いて、メールを確認済みとする必要がある。

実際のコード

SignUp.cs
    var userPool = new CognitoUserPool(UserPoolId, AppClientID, _provider);
    var user = new CognitoUser(email, AppClientID, userPool, _provider);
    var authRequest = new InitiateCustomAuthRequest
    {
        AuthParameters = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            {
                "USERNAME",
                "username"
            }
        },
        ClientMetadata = new Dictionary<string, string>(StringComparer.Ordinal)
    };

    var result = user.StartWithCustomAuthAsync(authRequest).ConfigureAwait(false).GetAwaiter()
        .GetResult();

    _sessionID = result.SessionID;
    _userName = result.ChallengeParameters["USERNAME"];
Confirm.cs
   var userPool = new CognitoUserPool(UserPoolId, AppClientID, _provider);
    var user = new CognitoUser(email, AppClientID, userPool, _provider);
    var authRequest = new RespondToCustomChallengeRequest
    {
        ChallengeParameters = new Dictionary<string, string>(StringComparer.Ordinal)
        {
            {
                "ANSWER",
                confirmationCode
            },
            {
                "USERNAME",
                _userName
            }
        },
        SessionID = _sessionID
    };
    var result = user.RespondToCustomAuthAsync(authRequest).ConfigureAwait(false).GetAwaiter().GetResult();
  

2
1
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
2
1