Home
Blog
Products
Profile
Study
Collatz
© 2024 Oizumi Yuta

【個人開発】ユーザー認証機能追加

2024-10-18

今日も開発を行った。

  • ユーザー認証機能追加

ユーザー認証は Cognito で行う。SDK をインストール。

npm install @aws-sdk/client-cognito-identity-provider

その後、サインアップ、サインイン、認証コードでの認証を実装した。以下はサインアップ:

import {
  SignUpCommand,
  CognitoIdentityProviderClient,
} from '@aws-sdk/client-cognito-identity-provider';

interface SignUpParams {
  email: string;
  password: string;
}

const signUp = async ({ email, password }: SignUpParams) => {
  const client = new CognitoIdentityProviderClient(config);

  const command = new SignUpCommand({
    ClientId: CLIENT_ID,
    Username: email,
    Password: password,
    UserAttributes: [
      {
        Name: 'email',
        Value: email,
      },
    ],
  });

  return client.send(command);
};

詳細は以下参照:

  • SignUpCommand
  • ConfirmSignUpCommand
  • InitiateAuthCommand

あとはユーザーとユーザーが作った絵本を紐づけるように DynamoDB テーブルに持たせる必要がある。

  • Cognito のユーザー ID を格納する userId キーを追加
  • アプリ内のユーザー名を格納する userName キーを追加

これをもとに絵本作成者のみ作成した絵本を編集・削除できるように実装する予定。