Amazon+Cognitoを触ってみた
ユーザープールを作成する
1.以下のコマンドを実行してユーザープールを作成します。 [test_user_pool]は適宜変更します。
aws cognito-idp create-user-pool \ --pool-name test_user_pool
2.以下のコマンドを実行してユーザープールが作成されたことを確認します。
aws cognito-idp list-user-pools \ --max-results 20
実行結果は以下のようになります。
IDを控えます。
実行結果例
---------------------------------------------------------------------------------------------
{
{
"Id": "ap-northeast-1_a01eEJWMX",
"Name": "test_user_pool",
"LambdaConfig": {},
"LastModifiedDate": "2021-01-02T01:02:50.670000+09:00",
"CreationDate": "2021-01-02T01:02:50.670000+09:00"
},
}
---------------------------------------------------------------------------------------------
アプリクライアントを追加する
1.以下のコマンドを実行してアプリクライアントを追加するための設定ファイルを作成します。 「create-user-pool-client.json」は任意です。
aws cognito-idp create-user-pool-client \ --generate-cli-skeleton > create-user-pool-client.json
vim create-user-pool-client.json
2.作成したjsonファイルを設定します。
設定するポイントは以下の通りです。適宜設定します。
UserPoolId → aws cognito-idp list-user-pools --max-results 20 で控えたIDを設定します。
ClientName → クライアント名を記載します。
CallbackURLs → リダイレクト先のURLを記載します。
LogoutURLs → ID プロバイダが許可するログアウト URL。
{
"UserPoolId": "ap-northeast-1_a01eEJWMX",
"ClientName": "testclient",
"GenerateSecret": false,
"RefreshTokenValidity": 30,
"ReadAttributes": [
"address",
"birthdate",
"email",
"email_verified",
"family_name",
"gender",
"given_name",
"locale",
"middle_name",
"name",
"nickname",
"phone_number",
"phone_number_verified",
"picture",
"preferred_username",
"profile",
"updated_at",
"website",
"zoneinfo"
],
"WriteAttributes": [
"address",
"birthdate",
"email",
"family_name",
"gender",
"given_name",
"locale",
"middle_name",
"name",
"nickname",
"phone_number",
"picture",
"preferred_username",
"profile",
"updated_at",
"website",
"zoneinfo"
],
"ExplicitAuthFlows": [
"ALLOW_CUSTOM_AUTH",
"ALLOW_REFRESH_TOKEN_AUTH",
"ALLOW_USER_SRP_AUTH"
],
"SupportedIdentityProviders": [
"COGNITO"
],
"CallbackURLs": [
"https://koishi.work"
],
"LogoutURLs": [
"https://koishi.work"
],
"AllowedOAuthFlows": [
"code",
"implicit"
],
"AllowedOAuthScopes": [
"aws.cognito.signin.user.admin",
"email",
"openid",
"phone",
"profile"
],
"AllowedOAuthFlowsUserPoolClient": true,
"PreventUserExistenceErrors": "ENABLED"
}
3.以下のコマンドを実行し、アプリクライアントを作成します。
aws cognito-idp create-user-pool-client \ --cli-input-json "file://create-user-pool-client.json"
以下のコマンドでアプリクライアントの詳細を確認できます。
aws cognito-idp list-user-pool-clients \ --user-pool-id ap-northeast-1_a01eEJWMX
aws cognito-idp describe-user-pool-client \ --user-pool-id ap-northeast-1_a01eEJWMX \ --client-id 3n75rg18sfgr6h22q285ilijki
4.以下のコマンドを実行し、Amazon Cognito ドメインを作成します。
aws cognito-idp create-user-pool-domain \ --user-pool-id ap-northeast-1_a01eEJWMX \ --domain test-koishi-domain
作成したドメインは以下のコマンドで確認できます。
aws cognito-idp describe-user-pool-domain \ --domain test-koishi-domain
5.サインインページを表示させてみます。 AWSのチュートリアルでは以下のように記載があります。分かりにくいので実際のURLも参考にしてください。 https://your_domain/login?response_type=token&client_id=your_app_client_id&redirect_uri=your_callback_url 実際のURL
https://test-koishi-domain.auth.ap-northeast-1.amazoncognito.com/login?response_type=token&client_id=3n75rg18sfgr6h22q285ilijki&redirect_uri=https://koishi.work
6.以下のコマンドを実行してユーザーを作成します。
aws cognito-idp sign-up --client-id 3n75rg18sfgr6h22q285ilijki \ --username koishi \ --password Passw0rd! \ --user-attributes Name=email,Value=koishi@admin.com
7.以下のコマンドを実行してユーザーをプールに登録します。
aws cognito-idp admin-confirm-sign-up \ --user-pool-id ap-northeast-1_a01eEJWMX \ --username koishi
8.作成したユーザーでサインインしてみます。 ページが遷移したら成功です。 ーー終わりにーー 今回はCognitoのチュートリアル通り(しかも途中)に作成しただけなので、 koishi.workのページが普通に見えてしまいます。 多分CloudFrontの設定などで認証しないと見えないようにするといったことをすると 思うので調べてみようと思います。 参考URL https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/getting-started-with-cognito-user-pools.html