如何在 AWS 中使用 AMAZON_COGNITO_USER_POOLS 在 ReactJS 中放大 API



我在 React 中有一个使用 AWS Amplify API 和身份验证的应用程序。我已经配置了API_KEY和AMAZON_COGNITO_USER_POOLS来访问 API。例如,我有一个这样的 graphql 模式:

type Course @model @auth(rules: [
# Defaults to use the "owner" field.
{allow: owner, ownerField: "author", provider: userPools, operations: 
[read, create, update, delete]},
# Admin users can access any operation.
{ allow: groups, groups: ["Admin"] },
# Next allow public access with an API Key
{allow: public, provider: apiKey, operations: [read]}
]){
id: ID!
title: String!
content: String!
author: String
}

我已经推送了我的更改 (amplify push(,但是当我尝试创建课程时,我遇到了通过 AWS Cognito 成功登录的用户Not Authorized to access createCourse on type Course

API.graphql({
query: createCourse,
variables: {input: {...course}},
authMode: 'AMAZON_COGNITO_USER_POOLS'
})

额外信息:
我正在使用联合登录 (Google(。用户访问该网站,他们通过谷歌登录。访问令牌可通过以下方式获得:Auth.currentSession()).getIdToken().getJwtToken()但是如果我必须对此令牌执行任何操作,或者它在 graphql API 调用中自动使用,则我不这样做。

在类型定义中,您有ownerField: "author",因此您应该将当前经过身份验证的用户传递给输入。

course= {
title: "titel...", 
content: "content...", 
author:user //Current Authenticated User
};


获取当前经过身份验证的用户对象

Auth.currentAuthenticatedUser({
bypassCache: false 
}).then(user => console.log(user))
.catch(err => console.log(err));

最新更新