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

Page updated Jun 20, 2024

サインイン済みユーザーのデータアクセス

authenticated 認可戦略は、IAM、Cognito、または OpenID Connect を通じて認証されたサインイン済みユーザーのみにレコードアクセスを制限し、認可ルールをすべてのユーザーに適用します。これは、認証されたすべてのユーザーのデータをプライベートにする簡単な方法を提供します。

サインイン済みユーザーの認可ルールを追加する

authenticated 認可戦略を使用して、レコードのアクセスをすべてのサインイン済みユーザーに制限できます。

注: レコードのアクセスを特定のユーザーに制限したい場合は、ユーザーごと/所有者ごとのデータアクセスを参照してください。このページで説明する authenticated 認可戦略は、すべてのサインイン済みユーザーにデータアクセスの認可ルールを適用します。

以下の例では、Cognito ユーザープールからの有効な JWT トークンを持つ誰もがすべての Todo にアクセスできます。

amplify/data/resource.ts
const schema = a.schema({
Todo: a
.model({
content: a.string(),
})
.authorization(allow => [allow.authenticated()]),
});

アプリケーションで、userPools 認証モードでモデルに対して CRUD 操作を実行できます。

try {
final todo = Todo(content: 'My new todo');
final request = ModelMutations.create(
todo,
authorizationMode: APIAuthorizationType.userPools,
);
final createdTodo = await Amplify.API.mutations(request: request).response;
if (createdTodo == null) {
safePrint('errors: ${response.errors}');
return;
}
safePrint('Mutation result: ${createdTodo.name}');
} on APIException catch (e) {
safePrint('Failed to create todo', e);
}

サインイン済みユーザー認証に ID プールを使用する

認可プロバイダーをオーバーライドすることもできます。以下の例では、identityPool がプロバイダーとして指定されており、API キーの代わりに Cognito ID プールから「未認証ロール」を使用してパブリックアクセスを許可できます。amplify/auth/resource.ts で定義された Auth リソースは、Cognito ID プールの「未認証ロール」に対してスコープダウンされた IAM ポリシーを自動的に生成します。

amplify/data/resource.ts
const schema = a.schema({
Todo: a
.model({
content: a.string(),
})
.authorization(allow => [allow.authenticated('identityPool')]),
});

アプリケーションで、iam 認証モードでモデルに対して CRUD 操作を実行できます。

try {
final todo = Todo(content: 'My new todo');
final request = ModelMutations.create(
todo,
authorizationMode: APIAuthorizationType.iam,
);
final createdTodo = await Amplify.API.mutations(request: request).response;
if (createdTodo == null) {
safePrint('errors: ${response.errors}');
return;
}
safePrint('Mutation result: ${createdTodo.name}');
} on APIException catch (e) {
safePrint('Failed to create todo', e);
}

さらに、authenticated 認可で OpenID Connect を使用することもできます。認可プロバイダーとしての OpenID Connectを参照してください。