Javascript Firebase电子邮件链接身份验证
我正在为一个项目使用无密码身份验证,一切都按预期进行,但是我对这种身份验证有一个问题。我将讨论这个场景 第一步:众所周知,新用户需要一封电子邮件,然后点击链接登录 这是正常情况,没有问题,但如果用户已经完成了这一步骤,并说他/她从应用程序中注销,该怎么办?似乎他们需要再次执行我上面描述的第一步 以下是我迄今为止所尝试的:Javascript Firebase电子邮件链接身份验证,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我正在为一个项目使用无密码身份验证,一切都按预期进行,但是我对这种身份验证有一个问题。我将讨论这个场景 第一步:众所周知,新用户需要一封电子邮件,然后点击链接登录 这是正常情况,没有问题,但如果用户已经完成了这一步骤,并说他/她从应用程序中注销,该怎么办?似乎他们需要再次执行我上面描述的第一步 以下是我迄今为止所尝试的: login() { const email = this.email; this.$store .dispatch("LOGIN", { email
login() {
const email = this.email;
this.$store
.dispatch("LOGIN", { email })
.then(resp => {
this.$router.replace("/");
})
.catch(err => {
this.autherror = true,
this.errorMessage = err.message;
});
}
LOGIN: ({ commit }, user) => {
return new Promise((resolve, reject) => {
// commit(AUTH_REQUEST)
firebase.auth().signInWithEmailLink(user.email, window.location.href)
.then((result) => {
window.localStorage.removeItem("emailForSignIn");
resolve(result);
})
.catch((err) => {
reject(err);
// Some error occurred, you can inspect the code: error.code
// Common errors could be invalid email and invalid or expired OTPs.
});
});
},
尝试上面的代码时,我会收到一个错误“无效的电子邮件链接!”,即使我将url作为以前登录的url,它也会抛出一个错误“”操作代码无效。如果代码格式错误、过期或已被使用,则可能会发生这种情况“
我可以理解为什么总是需要一封电子邮件来登录,但我想说的主要一点是,如果用户先从链接登录,然后再注销,他们可以登录应用程序而不需要再次执行第一步,如何?这意味着如果有办法将凭据存储在Cookie/localstorage中,并且他们唯一需要再次执行第一步的时间就是清除所有或特定应用程序/页面中的Cookie、storage等
那有可能吗?这肯定会改善用户体验。您应该阅读并了解用户在Firebase中的工作方式(在任何oAuth类型验证系统中基本相同)—— 更具体地说,电子邮件是如何使用的- 在您的代码中,您应该使用上面参考中所示的电子邮件确认步骤(因此,类似于下面的代码-您可能需要一些小的更改以适应您的本地场景):
谢谢你花时间,但这并没有回答我的问题,这只是已经起作用的。那么,请重新措辞这个问题这与您在问题中使用的代码不同,并且不应提供您在问题中描述的错误,这是您要求提供帮助的内容。如果它“已经工作了”,那么问题是什么?请阅读最后一段,其中解释说,正如我所说的,一切都按预期工作,只希望用户不需要每次通过电子邮件中的链接登录,如果他们第一次已经这样做了,而是存储凭据并登录,除非它们清除了Cookie/storage。如果您想“存储凭据”,则不要使用“removeItem”(“emailForSignIn”)——这将删除电子邮件,而这将在下次不可用……到目前为止有解决方案吗?
LOGIN: ({ commit }, user) => {
return new Promise((resolve, reject) => {
// Confirm the link is a sign-in with email link.
if (firebase.auth().isSignInWithEmailLink(window.location.href)) {
// Additional state parameters can also be passed via URL.
// This can be used to continue the user's intended action before triggering
// the sign-in operation.
// Get the email if available. This should be available if the user completes
// the flow on the same device where they started it.
var email = window.localStorage.getItem('emailForSignIn');
if (!email) {
// User opened the link on a different device. To prevent session fixation
// attacks, ask the user to provide the associated email again. For example:
email = window.prompt('Please provide your email for confirmation');
}
// commit(AUTH_REQUEST)
firebase.auth().signInWithEmailLink(email, window.location.href)
.then((result) => {
window.localStorage.removeItem("emailForSignIn");
resolve(result);
})
.catch((err) => {
reject(err);
// Some error occurred, you can inspect the code: error.code
// Common errors could be invalid email and invalid or expired OTPs.
});
});
}
},