Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Choose your framework/language

Gen1 DocsLegacy

Page updated Jun 20, 2024

カスタム識別情報およびグループクレームの設定

Amplify Dataは、デフォルトで提供されるAmazon Cognito の cognito:groups またはダブルコロン区切りのクレーム sub::username をJWTトークンから使用したくない場合に、カスタム識別情報およびグループクレームの使用をサポートしています。これは、サードパーティのOIDCシステムからのトークンを使用している場合、または事前トークン生成Lambdaトリガーを使用している場合など、データベースから読み込むグループのリストで外部システムからクレームを入力したい場合に便利です。

カスタムクレームを使用するには、必要に応じて identityClaim または groupClaim を指定します。以下の例では、identityClaim が指定され、レコードオーナーは この user_id クレームに対してチェックされます。同様に、user_groups クレームに「Moderator」文字列が含まれている場合、アクセスが許可されます。

amplify/data/resource.ts
import { a, defineData, type ClientSchema } from '@aws-amplify/backend';
const schema = a.schema({
Post: a
.model({
id: a.id(),
owner: a.string(),
postname: a.string(),
content: a.string(),
})
.authorization(allow => [
allow.owner().identityClaim('user_id'),
allow.groups(['Moderator']).withClaimIn('user_groups'),
]),
});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({ schema });

アプリケーション内では、userPool認証モードでclient.models.<model-name>を使用してモデルに対するCRUD操作を実行できます。

import { generateClient } from 'aws-amplify/data';
import type { Schema } from '../amplify/data/resource'; // Path to your backend resource definition
const client = generateClient<Schema>();
const { errors, data: newTodo } = await client.models.Todo.create(
{
postname: 'My New Post'
content: 'My post content',
},
{
authMode: 'userPool',
}
);