node.js - 执行经过身份验证的 keystone.js/GraphQL API 查询

标签 node.js authentication graphql keystonejs

我是 Keystone.js 和 GraphQL 的新手。到目前为止,我已经能够通过 POST 请求成功执行以下 API 查询(取自 page ):

const SIGNIN = `mutation signin($identity: String, $secret: String) {
  authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
    item {
      id
      name
    }
  }
}`;

// Returns id and name of authenticated user

const GET_ALL_POSTS = `query GetPosts {
  allPosts {
    name
    id
  }
}`;

// Returns id and name of all posts (if no access controls)

如果我设置access controls对于列表 Post 我按预期从第二个查询中收到访问错误,但我无法了解如何对 allPosts 执行经过身份验证的查询,例如我想要:

  • 通过电子邮件和密码(以编程方式)对用户进行身份验证
  • 如果成功,则运行 allPosts 查询并返回结果

我做错了什么?

最佳答案

答案是首先提交下面的查询,以验证您的用户并生成访问 token :

query: `mutation ($identity: String, $secret: String) {
  authenticate: authenticateUserWithPassword(email: $identity, password: $secret) {
    token
  }
}`

然后通过将提供的 token 添加到请求的 header 中来对后续查询进行身份验证:

'Authorization: Bearer <token>'.

更多信息here .

更一般地说,the GraphQL playground对于任何进入 keystonejs 和 GraphQL 新手的人来说都非常值得一看。

关于node.js - 执行经过身份验证的 keystone.js/GraphQL API 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077526/

相关文章:

iPhone Facebook 登录屏幕问题

php - 登录后显示用户名

graphql - GraphQL 中聚合函数的支持

react-native - Apollo Refetch() 后组件 `props.data` 不会重新加载

java - JAX-RS REST 服务如何通过注释处理身份验证?

node.js - 如何仅在经过身份验证后才提供 session ?任何其他中间件命令都不起作用

node.js - npm 发布一个仅包含 dist 文件夹子项的包

javascript - AWS Lambda - Nodejs : Allocation failed - JavaScript heap out of memory

javascript - 如何正确使用 GraphQL 订阅?

node.js - NodeJS Azure 网站在 HTTP POST 上返回 405