Ric*_*. M 20 amazon-web-services amazon-cognito aws-sdk aws-cognito
我想在亚马逊认知用户确认后重定向到特定网址.
当用户注册时,他将收到带有验证链接的确认邮件,如下所示 :https:// <> .auth.us-west-2.amazoncognito.com/confirmUser?client_id = << >>&user_name = << >>&confirmation_code = << >>
如果用户点击上述链接,它将重定向到确认页面.
用户确认完成后,页面应重定向到我的应用程序.
请给我一些解决这个问题的想法.
age*_*420 16
目前,使用电子邮件中的验证链接无法完成此重定向.我曾尝试将redirect_uri添加到验证网址,但它们不起作用.
解决方法
这些值传递给后端lambda,后者发出GET请求到https://your_domain.auth.us-west-2.amazoncognito.com/confirmUser?client_id=somevalue&user_name=some_user&confirmation_code=some_code
成功后,从您的API网关返回302 https://myapp.com
我知道这对于这样一个简单的要求来说是一个复杂的解决方法.最好的方法是提出功能请求,并希望他们支持Cognito URL中的redirect_uri.
编辑
要节省lambda成本,您还可以在API中使用HTTP端点,并向您所在地区的cognito服务端点发出请求.例:
POST HTTP/1.1
Host: cognito-idp.us-east-1.amazonaws.com
x-amz-target: AWSCognitoIdentityProviderService.ConfirmSignUp
Content-Type: application/x-amz-json-1.1
{
"ClientId":"xxxxxxxxxxxxx",
"ConfirmationCode":"123456",
"Username":"username"
}
Run Code Online (Sandbox Code Playgroud)
Yas*_*aka 10
我在@agent420 的上述答案的帮助下得到了这个,并检查了 github 问题https://github.com/aws-amplify/amplify-js/issues/612
所以这是我遵循的完整过程。
exports.handler = (event, context, callback) => {
// Identify why was this function invoked
if(event.triggerSource === "CustomMessage_SignUp") {
console.log('function triggered');
console.log(event);
// Ensure that your message contains event.request.codeParameter. This is the placeholder for code that will be sent
const { codeParameter } = event.request
const { userName, region } = event
const { clientId } = event.callerContext
const { email } = event.request.userAttributes
const url = 'https://example.com/api/dev/user/confirm'
const link = `<a href="${url}?code=${codeParameter}&username=${userName}&clientId=${clientId}®ion=${region}&email=${email}" target="_blank">here</a>`
event.response.emailSubject = "Your verification link"; // event.request.codeParameter
event.response.emailMessage = `Thank you for signing up. Click ${link} to verify your email.`;
}
// Return to Amazon Cognito
callback(null, event);
};Run Code Online (Sandbox Code Playgroud)
您的电子邮件将与主体和指定的消息发送event.response.emailSubject和event.response.emailMessage。用户将被定向到url变量中指定的 url 。
我通过 AWS APIGateway 为此使用了 lambda。下面给出的是我在 nodejs 中编写的代码,其中使用了 301 重定向。
'use strict';
var AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: process.env.REGION });
module.exports.verifyEmailAddress = (req, context, callback) => {
console.log('req');
console.log(req);
const confirmationCode = req.queryStringParameters.code
const username = req.queryStringParameters.username
const clientId = req.queryStringParameters.clientId
const region = req.queryStringParameters.region
const email = req.queryStringParameters.email
let params = {
ClientId: clientId,
ConfirmationCode: confirmationCode,
Username: username
}
var confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise()
confirmSignUp.then(
(data) => {
let redirectUrl = process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL;
const response = {
statusCode: 301,
headers: {
Location: redirectUrl,
}
};
return callback(null, response);
}
).catch(
(error) => {
callback(error)
}
)
}Run Code Online (Sandbox Code Playgroud)
根据需要替换环境变量REGION和POST_REGISTRATION_VERIFICATION_REDIRECT_URL您的值。
完整文章链接:https : //medium.com/@jacobjoy/redirect-user-using-amazon-cognito-confirmation-url-d8ccb11bac75
谢谢@yasith 从简单的图像开始,以明确我们是如何进行这项工作的,
第 1 步:使用您选择的语言创建一个 lambda 函数,我将在以下示例中使用 node.js。请在继续之前阅读自定义消息 Lambda 触发器,https:
//docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-custom-message.html 在这个例子中,我只处理 CustomMessage_SignUp 你可以根据您的要求自定义其他人,如 CustomMessage_ForgotPassword 等。将以下代码复制到您的 lambda 函数中,
// Creating a custom URL for the user
exports.handler = (event, context, callback) => {
if(event.triggerSource === "CustomMessage_SignUp") {
const { codeParameter } = event.request;
const { userName, region } = event;
const { clientId } = event.callerContext;
const { email } = event.request.userAttributes;
const url = 'https://xxxxxxx.execute-api.eu-west-2.amazonaws.com/prod/redirect'
const link = `<a href="${url}?code=${codeParameter}&username=${userName}&clientId=${clientId}®ion=${region}&email=${email}" target="_blank">Click the link to verify</a>`;
event.response.emailSubject = "Your verification link";
event.response.emailMessage = `Thank you for signing up. Click ${link} to verify your email.`;
}
// CallBack to the lambda for the email trigger
callback(null, event);
};
Run Code Online (Sandbox Code Playgroud)
注意:一旦您设置了 API 网关,就应该更新 const URL。
第 2 步:在 Cognito Trigger 下选择自定义消息并选择您创建的 lambda 函数
第 3 步:在您的 API 网关中创建 GET API 不要设置任何授权或不要启用任何所需的 API 密钥。
'use strict';
var AWS = require('aws-sdk');
AWS.config.setPromisesDependency(require('bluebird'));
var CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({
apiVersion: '2019-11-07',
region: process.env.REGION
});
exports.handler = (req, context, callback) => {
console.log(req);
const confirmationCode = req.code;
const username = req.username;
const clientId = req.clientId;
let params = {
ClientId: clientId,
ConfirmationCode: confirmationCode,
Username: username
};
//Validating the user
let confirmSignUp = CognitoIdentityServiceProvider.confirmSignUp(params).promise();
//Returning the redirect url
confirmSignUp.then(
(data) => {
context.succeed({
location: process.env.POST_REGISTRATION_VERIFICATION_REDIRECT_URL
});
}
).catch(
(error) => {
callback(error.message)
}
)
};
Run Code Online (Sandbox Code Playgroud)
在您选择的 IDE 中创建一个节点应用程序,然后使用 npm install 进行构建,一旦您的应用程序构建完成。创建一个 zip(在根文件夹中大约有 5 个文件基于您的 IDE)并上传到您的 lambda 应用程序。
Lambda Handler 方法应该是 = index.handler
在 lambda env 变量中设置以下内容
POST_REGISTRATION_VERIFICATION_REDIRECT_URL 区域
在您的应用程序中,您正在验证您的认知用户池中的用户并返回重定向的 URL。不在范围内:也可以根据您的要求处理错误场景。
Step5:回到API网关,创建一个GET请求并放入你在Step4中创建的Lambda函数并更新映射模板。
Step6:现在我们必须根据您的 lambda 响应重定向请求,在 API Gateway 中的 Method Response 下删除 200 并创建一个 302 并根据图像添加响应标头 Location,
然后在集成响应中,您需要删除 200 并添加 302,然后将标头映射值添加为integration.response.body.location(注意字符大小写)

最重要的步骤:完成所有设置后部署 API 并更新在步骤 1 中创建的 Lambda 函数中的 const URL API。
尝试创建一个测试用户,用户必须有这样的电子邮件链接, https ://xxxx-api.eu-west-2.amazonaws.com/xx/xx/xx/redirect?code=xx &username=xxxxx;clientId =xxxxx;region=eu-west-2&email=test@gmail.com
| 归档时间: |
|
| 查看次数: |
6783 次 |
| 最近记录: |