0

所以目前我使用 AWS Lambdas 作为我的 Cognito Passwordless 身份验证的触发器。对于 create_auth_challenge 触发器,我有一个 AWS Lambda 函数,该函数向用户发送一个链接,以根据环境将他们重定向到某个地方。唯一的问题是我不确定如何动态地告诉函数身份验证请求来自哪个环境。

AWS.config.update({ region: 'us-west-2' });
const SES = new AWS.SES();
exports.handler = async (event,context) => {
    console.log("HERE: ", event,context);
    let secretLoginCode;
    if (!event.request.session || !event.request.session.length) {
        // Generate a new secret login code and send it to the user
        secretLoginCode = Date.now().toString().slice(-4);
        try {
            if ('email' in event.request.userAttributes) {
                const emailResult = await SES.sendEmail({
                    Destination: { ToAddresses: [event.request.userAttributes.email] },
                    Message: {
                        Body: {
                            Html: {
                                Charset: 'UTF-8',
                                Data: `<html><body><p>This is your secret login code:</p>
                           <h3>Your magic link: ${INSERT ENVIRONMENT HERE}/api/auth/cognito/verify?email=${event.request.userAttributes.email}&code=${secretLoginCode}</h3></body></html>`
                            },
                            Text: {
                                Charset: 'UTF-8',
                                Data: `Your magic link: ${INSERT ENVIRONMENT HERE}/api/auth/cognito/verify?email=${event.request.userAttributes.email}&code=${secretLoginCode}`
                            }
                        },
                        Subject: {
                            Charset: 'UTF-8',
                            Data: 'Your magic link'
                        }
                    },
                    Source: 'Company <no-reply@company.com>'
                }).promise();
            }
        } catch (error) {
            console.log(error)
        }
    } else {
        // re-use code generated in previous challenge
        const previousChallenge = event.request.session.slice(-1)[0];
        secretLoginCode = previousChallenge.challengeMetadata.match(/CODE-(\d*)/)[1];
    }
    // Add the secret login code to the private challenge parameters
    // so it can be verified by the "Verify Auth Challenge Response" trigger
    event.response.privateChallengeParameters = { secretLoginCode };
    // Add the secret login code to the session so it is available
    // in a next invocation of the "Create Auth Challenge" trigger
    event.response.challengeMetadata = `CODE-${secretLoginCode}`;
    return event;
};```

This is a magic link authentication by the way.
4

0 回答 0