AWS Cognito:处理来自不同身份提供商(Google,Facebook)的同一用户(具有相同电子邮件地址)登录的最佳实践 [英] AWS Cognito: Best practice to handle same user (with same email address) signing in from different identity providers (Google, Facebook)

本文介绍了AWS Cognito:处理来自不同身份提供商(Google,Facebook)的同一用户(具有相同电子邮件地址)登录的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Google和Facebook身份提供商使用相同电子邮件地址登录用户时,AWS Cognito在用户池中创建多个条目,每个使用的身份提供商一个条目:





我已使用本教程中提供的示例代码来设置AWS Cognito:使用Amplify框架进行用户身份验证的完整指南




  • 如何仅创建一个用户而不是多个用户?

  • 是能否让AWS Cognito自动将(多个)来自多个提供程序的条目
    组合(联合)到一个条目中,或者应该使用AWS Lambda函数来完成此操作?


解决方案

是。您可以使用 AdminLinkProviderForUser https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminLinkProviderForUser.html



想法是:


  1. 在PreSignUp lambda挂钩中,如果用户已经注册,我们会将提供程序链接到用户。例如:



 从'aws-sdk / clients导入CognitoIdentityServiceProvider / cognitoidentityserviceprovider'

const cognitoIdp = new CognitoIdentityServiceProvider()
const getUserByEmail = async(userPoolId,email)=> {
const params = {
UserPoolId:userPoolId,
过滤器:`email = $ {email}`
}
返回cognitoIdp.listUsers(params)。 promise()
}

const linkProviderToUser =异步(用户名,userPoolId,providerName,providerUserId)=> {
const params = {
DestinationUser:{
ProviderAttributeValue:用户名,
ProviderName:'Cognito'
},
SourceUser:{
ProviderAttributeName: Cognito_Subject,
ProviderAttributeValue:providerUserId,
ProviderName:ProviderName
},
UserPoolId:userPoolId
}

const结果=等待(新的Promise((resolve,reject)= >> {
cognitoIdp.adminLinkProviderForUser(params,(err,data)=> {
if(err){
reject(err)
return
}
resolve(data)
})
}))

返回结果
}

exports.handler =异步(事件,上下文,回调)=> {
if(event.triggerSource ==='PreSignUp_ExternalProvider'){
const userRs = await getUserByEmail(event.userPoolId,event.request.userAttributes.email)
if(userRs&& ; userRs.Users.length> 0){
const [providerName,providerUserId] = event.userName.split('_')//事件userName示例: Facebook_12324325436
等待linkProviderToUser(userRs。 Users [0] .Username,event.userPoolId,providerName,providerUserId)
} else {
console.log('未找到用户,跳过。')
}

}
返回回调(空,事件)
}




  1. 然后,当用户在带有用户池的Facebook / Google上使用OAuth时,池将返回此用户链接。

注意:您可能在用户池UI中看到2条记录,但是在访问用户记录详细信息时,它们已经合并。


When signing in a user with the same email address through the Google and Facebook identity providers, AWS Cognito creates multiple entries in the user pool, one entry per identity provider used:

I have used the example code provided in this tutorial to set up AWS Cognito: The Complete Guide to User Authentication with the Amplify Framework

  • How can I create just one user instead of multiple users?
  • Is it possible to have AWS Cognito automatically combine (federate) the entries from multiple providers into one entry or should AWS Lambda functions be used to accomplish this?

解决方案

Yes. You can do it by using AdminLinkProviderForUser https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminLinkProviderForUser.html

The idea is:

  1. In PreSignUp lambda hook, we Link Provider to User if User already signed up. E.g:

import CognitoIdentityServiceProvider from 'aws-sdk/clients/cognitoidentityserviceprovider'

const cognitoIdp = new CognitoIdentityServiceProvider()
const getUserByEmail = async (userPoolId, email) => {
 const params = {
   UserPoolId: userPoolId,
   Filter: `email = "${email}"`
 }
 return cognitoIdp.listUsers(params).promise()
}

const linkProviderToUser = async (username, userPoolId, providerName, providerUserId) => {
 const params = {
   DestinationUser: {
     ProviderAttributeValue: username,
     ProviderName: 'Cognito'
   },
   SourceUser: {
     ProviderAttributeName: 'Cognito_Subject',
     ProviderAttributeValue: providerUserId,
     ProviderName: providerName
   },
   UserPoolId: userPoolId
 }

 const result = await (new Promise((resolve, reject) => {
   cognitoIdp.adminLinkProviderForUser(params, (err, data) => {
     if (err) {
       reject(err)
       return
     }
     resolve(data)
   })
 }))

 return result
}

exports.handler = async (event, context, callback) => {
 if (event.triggerSource === 'PreSignUp_ExternalProvider') {
   const userRs = await getUserByEmail(event.userPoolId, event.request.userAttributes.email)
   if (userRs && userRs.Users.length > 0) {
     const [ providerName, providerUserId ] = event.userName.split('_') // event userName example: "Facebook_12324325436"
     await linkProviderToUser(userRs.Users[0].Username, event.userPoolId, providerName, providerUserId)
   } else {
     console.log('user not found, skip.')
   }

 }
 return callback(null, event)
}

  1. Then when user use OAuth with Facebook/Google with User Pool, the Pool will return this User linked.

Note: You may see 2 records in User Pool UI, but when access User record detail, They already merged.

这篇关于AWS Cognito:处理来自不同身份提供商(Google,Facebook)的同一用户(具有相同电子邮件地址)登录的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆