【问题标题】:Perform an authenticated keystone.js / GraphQL API query执行经过身份验证的 keystone.js / GraphQL API 查询
【发布时间】:2020-11-14 13:16:30
【问题描述】:

我是 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)

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

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

我做错了什么?

【问题讨论】:

    标签: node.js authentication graphql keystonejs


    【解决方案1】:

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

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

    然后通过将提供的令牌添加到您的请求标头来验证后续查询:

    'Authorization: Bearer <token>'.
    

    更多信息here

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

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2022-06-19
      • 2021-05-01
      • 1970-01-01
      • 2019-09-25
      • 2021-03-01
      • 2013-01-27
      • 2021-02-23
      • 2016-12-17
      相关资源
      最近更新 更多