不了解 AWS Amplify 身份验证流程

not understanding AWS Amplify authentication flow

我正在尝试使用类似于以下摘自亚马逊网站的代码创建自定义 AWS Amplify 身份验证流程:

import { Auth } from 'aws-amplify';

async function signUp() {
    try {
        const user = await Auth.signUp({
            username,
            password,
            attributes: {
                email,          // optional
                phone_number,   // optional - E.164 number convention
                // other custom attributes 
            }
        });
        console.log({ user });
    } catch (error) {
        console.log('error signing up:', error);
    }
}

我对此有一些疑问。

  1. 令牌是如何刷新的?如果我将凭据保存在用户变量中,这是否会在幕后以某种方式自动刷新?

  2. 添加 graphql api 后如何发出经过身份验证的 graphql 请求?通过预先设置的身份验证流程,它会自动跟踪您的身份。这种登录方式怎么办?

谢谢!

  1. 是的,它们会在必要时刷新,除非您使用的是不同的社交提供商。您可以在他们的文档中找到更多信息 here, here and here

By default, Amplify will automatically refresh the tokens for Google and Facebook, so that your AWS credentials will be valid at all times.

...

you do not need to refresh Amazon Cognito tokens manually. The tokens are automatically refreshed by the SDK when necessary.

...

use Auth.signUp and Auth.signIn (or an Amplify UI component) to complete this process and retrieve tokens. The Amplify client will refresh the tokens calling Auth.currentSession if they are no longer valid.

  1. 当您实例化 AppSync 客户端时,您定义了它将如何检索令牌 即:
    const client = new AWSAppSyncClient({
        url: config.aws_appsync_graphqlEndpoint,
        region: config.aws_appsync_region,
        auth: {
          type: config.aws_appsync_authenticationType,
          apiKey: config.aws_appsync_apiKey,
          jwtToken: async () => (await Auth.currentSession()).getIdToken().getJwtToken(),
        },
    });