Amazon web services 如何使用Amazon Cognito创建无缝无密码注册/登录?
我有一个在Vue中运行的应用程序,它使用AWS Amplify和AWS Cognito identity sdk的混合。我的目标是创建无密码登录和注册。我现在已经让它工作到用户可以注册的程度,他们会得到一个电子邮件链接并点击它,然后它会将他们发送回带有url查询中验证码的站点 然后,当我在代码中解析该查询以进行身份验证时,我遇到了一个错误,使我陷入了一个解决方案的兔子洞。我的实现使用与这里相同的方法 我面临的问题是,当我找回代码时,我会将其发送到Cognito中的挑战答案,然后返回一个空会话。请参阅下面的代码 发送电子邮件的注册代码:Amazon web services 如何使用Amazon Cognito创建无缝无密码注册/登录?,amazon-web-services,vue.js,aws-lambda,amazon-cognito,aws-amplify,Amazon Web Services,Vue.js,Aws Lambda,Amazon Cognito,Aws Amplify,我有一个在Vue中运行的应用程序,它使用AWS Amplify和AWS Cognito identity sdk的混合。我的目标是创建无密码登录和注册。我现在已经让它工作到用户可以注册的程度,他们会得到一个电子邮件链接并点击它,然后它会将他们发送回带有url查询中验证码的站点 然后,当我在代码中解析该查询以进行身份验证时,我遇到了一个错误,使我陷入了一个解决方案的兔子洞。我的实现使用与这里相同的方法 我面临的问题是,当我找回代码时,我会将其发送到Cognito中的挑战答案,然后返回一个空会话。
async identitySignUp({ commit }, payload) {
console.log(commit)
let password =
randomCharGenerator(8, "lower") +
randomCharGenerator(8, "upper") +
randomCharGenerator(8, "number") +
randomCharGenerator(8, "symbol")
await Auth.signUp(payload, password)
.then(data => {
console.log(data)
Auth.signIn(data.user.username)
localStorage.setItem(
"cognitoUser",
JSON.stringify({
username: payload,
session: data.user.Session
})
)
})
.catch(e => {
if (e.code && e.code === "UsernameExistsException") {
console.log(e)
} else {
console.log(e)
}
})
// console.log(user, commit)
}
在上述代码中,有效负载是电子邮件。当I console.log退出此代码时:
await Auth.signUp(payload, password)
.then(data => {
console.log(data)
当用户没有会话时,我会得到奇怪的结果:
codeDeliveryDetails: undefined
user: CognitoUser
Session: null
authenticationFlowType: "USER_SRP_AUTH"
client: Client {endpoint: "https://cognito-idp.eu-west-2.amazonaws.com/", fetchOptions: {…}}
然后我有一个从url查询中获取代码的函数:
async answerChallenge(answer) {
try {
this.loading = true
await this.$store.dispatch("identity/answerCustomChallenge", answer)
this.loading = false
} catch (error) {
this.loading = false
this.$store.dispatch(
"errors/ERRORS_ADD",
Object.assign(error, { type: errorTypes.AUTH_LOGIN }),
{ root: true }
)
}
},
async getVerificationCode() {
let validationcode = this.$route.query.validation
// let email = this.$route.query.email
if (validationcode) {
this.answerChallenge(validationcode)
}
}
正如您所知,我从url获取代码,并将其传递给challengeAnswer,后者将传递给Cognito。我在这里面临的问题是,当我通过这个答案时,我会得到一个奇怪的错误:
code: "InvalidParameterException"
message: "Missing required parameter Session"
name: "InvalidParameterException"
这链接到用户中缺少的会话。下面是将代码发送到lambda的challengeAnswer函数:
async answerCustomChallenge({ commit }, payload) {
console.log(commit)
const user = JSON.parse(localStorage.getItem("cognitoUser"))
console.log(user)
// rehydrate the CognitoUser
const authenticationData = {
Username: user.username
}
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
authenticationData
)
console.log(authenticationDetails)
const poolData = {
UserPoolId: awsConfig.aws_user_pools_id,
ClientId: awsConfig.aws_user_pools_web_client_id
}
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData)
const userData = {
Username: user.username,
Pool: userPool
}
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData)
console.log(cognitoUser)
// After this set the session to the previously stored user session
cognitoUser.Session = user.session
// rehydrating the user and sending the auth challenge answer directly will not
// trigger a new email
cognitoUser.setAuthenticationFlowType("CUSTOM_AUTH")
// Auth.sendCustomChallengeAnswer(payload)
cognitoUser.sendCustomChallengeAnswer(payload, {
async onSuccess(success) {
console.log(success)
// If we get here, the answer was sent successfully,
// but it might have been wrong (1st or 2nd time)
// So we should test if the user is authenticated now
try {
// This will throw an error if the user is not yet authenticated:
const user = await Auth.currentSession()
console.log(user)
} catch {
console.error("Apparently the user did not enter the right code")
}
},
onFailure(failure) {
console.error(failure)
}
})
}
有人能告诉我他们认为我可能出了问题吗