【发布时间】:2021-10-21 19:46:13
【问题描述】:
我正在实现 cognito 中具有相同电子邮件的用户帐户的链接。因此,如果有人注册,例如使用 Google 并且电子邮件已经在 cognito 中,我将使用 AdminLinkProviderForUser 将这个新帐户链接到现有帐户。我基本上一直在这里关注这个答案:https://stackoverflow.com/a/59642140/13432045。链接按预期工作,但之后email_verified 切换到false(之前已验证)。这是预期的行为吗?如果是,那么我的问题是为什么?如果不是,那么我的问题是我做错了什么?这是我的预注册 lambda:
const {
CognitoIdentityProviderClient,
AdminLinkProviderForUserCommand,
ListUsersCommand,
AdminUpdateUserAttributesCommand,
} = require("@aws-sdk/client-cognito-identity-provider");
exports.handler = async (event, context, callback) => {
if (event.triggerSource === "PreSignUp_ExternalProvider") {
const client = new CognitoIdentityProviderClient({
region: event.region,
});
const listUsersCommand = new ListUsersCommand({
UserPoolId: event.userPoolId,
Filter: `email = "${event.request.userAttributes.email}"`,
});
try {
const data = await client.send(listUsersCommand);
if (data.Users && data.Users.length) {
const [providerName, providerUserId] = event.userName.split("_"); // event userName example: "Facebook_12324325436"
const provider = ["Google", "Facebook", "SignInWithApple"].find(
(p) => p.toUpperCase() === providerName.toUpperCase()
);
const linkProviderCommand = new AdminLinkProviderForUserCommand({
DestinationUser: {
ProviderAttributeValue: data.Users[0].Username,
ProviderName: "Cognito",
},
SourceUser: {
ProviderAttributeName: "Cognito_Subject",
ProviderAttributeValue: providerUserId,
ProviderName: provider,
},
UserPoolId: event.userPoolId,
});
await client.send(linkProviderCommand);
/* fix #1 - this did not help */
// const emailVerified = data.Users[0].Attributes.find(
// (a) => a.Name === "email_verified"
// );
// if (emailVerified && emailVerified.Value) {
// console.log("updating");
// const updateAttributesCommand = new AdminUpdateUserAttributesCommand({
// UserAttributes: [
// {
// Name: "email_verified",
// Value: "true",
// },
// ],
// UserPoolId: event.userPoolId,
// Username: data.Users[0].Username,
// });
// await client.send(updateAttributesCommand);
// }
/* fix #2 - have no impact on the outcome */
// event.response.autoConfirmUser = true;
// event.response.autoVerifyEmail = true;
}
} catch (error) {
console.error(error);
}
}
callback(null, event);
};
如您所见,我尝试传递 autoConfirmUser 和 autoVerifyEmail,但没有任何影响。而且我还尝试在调用AdminLinkProviderForUser 之后手动更新email_verified,这也没有帮助。所以我认为 email_verified 只有在 lambda 完成后才设置为 false。
【问题讨论】:
标签: amazon-web-services aws-lambda amazon-cognito federated-identity