0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[AWS Cognito] 招待メール経由登録に絞りつつ、Googleアカウントでログインできるようにする方法

Posted at

やりたいこと

Cognitoを用いたシングルサインオン
Googleアカウントからのログインをサポートしつつ、招待メール経由での登録のみ通す
(誰でも登録できては困るので)

ハマった点

Cognito経由での招待 -> 各ユーザがgoogleでログイン、とすると
「Cognitoで作成したユーザ」と、「googleサインアップ時に作成されたユーザー」で
同一メアドのユーザーがユーザープールに2つできてしまう。これらを統合する仕組みが必要

解決方法

招待メールを送った時点でuser pollにuserは作成されてしまうので、
その後googleログインする際にメアドで照合して統合する。他idp(facebookなど)でも同じようにいけるかなと思います

基本的には以下記事のコードを参考に実装できました。

(招待されていないメアドの場合、以下のように検索時に弾くように変更)

    # --- 3. 既存ユーザー検索 ---
    try:
        response = cognito_client.list_users(
            UserPoolId=user_pool_id,
            Filter=f'email = "{email_address}"'
        )
        users = response.get('Users', [])
        print(f"[DEBUG] Found users with same email: {len(users)}")
    except Exception as e:
        print(f"[ERROR] Error while listing users: {str(e)}")
        raise Exception(f"ユーザー検索に失敗しました: {str(e)}")

    if len(users) != 1:
        print(f"[WARN] No unique match for invited user with email: {email_address}")
        raise Exception("このメールアドレスに該当する招待ユーザーが見つからない、または一意に特定できません。")

    existing_user = users[0]
    existing_username = existing_user['Username']
    print(f"[INFO] Linking to existing user: {existing_username}")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?