【问题标题】:How to mock/generate cognito jwt token in unit tests?如何在单元测试中模拟/生成 cognito jwt 令牌?
【发布时间】:2019-06-09 02:02:45
【问题描述】:

在测试中模拟/生成 Cognito 令牌的最佳方法是什么?

我有一个设置授权标头的辅助函数:

const response = await request(service)
    .post('/graphql')
    .send({ query, variables })
    .set('Authorization', `Bearer ${token}`)

似乎我需要自己生成令牌,但我不确定如何生成,或者是否可以生成。

【问题讨论】:

    标签: javascript unit-testing jwt amazon-cognito aws-amplify


    【解决方案1】:

    您可以使用Auth.signIn 函数以编程方式登录并从 Cognito 接收 jwt 令牌。 有点像

    import { Auth } from 'aws-amplify'
    
    const signIn = Auth.signIn({ username, password })
    signIn.then(cognitoResponse => {
      // this is the token you need
      console.log(cognitoResponse.signInUserSession.accessToken.jwtToken);
    })
    

    Auth.signIn 文件:

    https://aws-amplify.github.io/amplify-js/api/classes/authclass.html#signin

    【讨论】:

      猜你喜欢
      • 2021-08-08
      • 2020-05-21
      • 2021-11-10
      • 2022-07-28
      • 1970-01-01
      • 2023-03-10
      • 2015-05-12
      • 2019-10-13
      • 2017-09-21
      相关资源
      最近更新 更多