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

Page updated May 2, 2024

管理アクションを使用

Amplify Authは、AWS SDKの@aws-sdk/client-cognito-identity-providerパッケージを使用して管理できます。このパッケージはサーバー側で使用することを目的としており、Functionの中で使用できます。この例ではaddUserToGroupアクションに焦点を当て、カスタムミューテーションとして定義されます。

開始するには、ミューテーションを認可するために使用する「ADMINS」グループを作成します。

amplify/auth/resource.ts
import { defineAuth } from "@aws-amplify/backend"
export const auth = defineAuth({
loginWith: {
email: true,
},
groups: ["ADMINS"]
})

次に、Functionリソースを作成します。

amplify/data/add-user-to-group/resource.ts
import { defineFunction } from "@aws-amplify/backend"
export const addUserToGroup = defineFunction({
name: "add-user-to-group",
})

次に、authリソースで、関数がaddUserToGroupアクションを実行するアクセス権を付与します。authリソースへのアクセス権の付与について詳しく学びます

amplify/auth/resource.ts
import { defineAuth } from "@aws-amplify/backend"
import { addUserToGroup } from "../data/add-user-to-group/resource"
export const auth = defineAuth({
loginWith: {
email: true,
},
groups: ["ADMINS"],
access: (allow) => [
allow.resource(addUserToGroup).to(["addUserToGroup"])
],
})

これでカスタムミューテーションを定義する準備ができました。ここでは、新しく作成したaddUserToGroup関数リソースを使用してaddUserToGroupミューテーションを処理します。このミューテーションは「ADMINS」グループのユーザーのみが呼び出すことができます。

amplify/data/resource.ts
import type { ClientSchema } from "@aws-amplify/backend"
import { a, defineData } from "@aws-amplify/backend"
import { addUserToGroup } from "./resource"
const schema = a.schema({
addUserToGroup: a
.mutation()
.arguments({
userId: a.string().required(),
groupName: a.string().required(),
})
.authorization((allow) => [allow.group("ADMINS")])
.handler(a.handler.function(addUserToGroup))
.returns(a.json())
})
export type Schema = ClientSchema<typeof schema>
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "iam",
},
})

最後に、エクスポートされたクライアントスキーマを使用してハンドラー関数をタイプ指定し、生成されたenvを使用してインタラクトするユーザープールIDを指定する関数のハンドラーを作成します。

amplify/data/add-user-to-group/handler.ts
import type { Schema } from "../resource"
import { env } from "$amplify/env/add-user-to-group"
import {
AdminAddUserToGroupCommand,
CognitoIdentityProviderClient,
} from "@aws-sdk/client-cognito-identity-provider"
type Handler = Schema["addUserToGroup"]["functionHandler"]
const client = new CognitoIdentityProviderClient()
export const handler: Handler = async (event) => {
const { userId, groupName } = event.arguments
const command = new AdminAddUserToGroupCommand({
Username: userId,
GroupName: groupName,
UserPoolId: env.AMPLIFY_AUTH_USERPOOL_ID,
})
const response = await client.send(command)
return response
}

フロントエンドでは、生成されたクライアントを使用してグループ名とユーザーのIDを使用するミューテーションを呼び出します。

src/client.ts
import type { Schema } from "../amplify/data/resource"
import { generateClient } from "aws-amplify/data"
const client = generateClient<Schema>()
await client.mutations.addUserToGroup({
groupName: "ADMINS",
userId: "5468d468-4061-70ed-8870-45c766d26225",
})