【发布时间】:2019-07-17 10:48:15
【问题描述】:
我使用 ionic 4 和 firebase/angularfire2 并使用 signinwithemeailandpassword() 方法进行身份验证,所以我需要在注册后第一次检查用户登录,所以我发现 firebase 使用 isNewUser 提供了该选项
因此,当使用createUserWithEmailAndPassword() 创建帐户时,isNewUser 应为equal 到true,因为它应该或正常!但是,当使用signinwithemeailandpassword() 登录时,无论第一次登录与否,总是返回 false!
这是登录方法的代码:
async login(form: NgForm) {
// checking if login is valid
if (form.invalid)
return this.statusMessage.message('Validation error!');
console.log(this.user);
try {
const results = await this._afAuth.auth.signInWithEmailAndPassword(this.user.email, this.user.password).then(
user => console.log(user.additionalUserInfo.isNewUser)
);
// console.log(results);
} catch (error) {
switch (error.code) {
case "auth/user-not-found":
this.statusMessage.message("Your email address is wrong! please try again");
break;
case "auth/invalid-email":
this.statusMessage.message("You have enterd invalid email address");
break;
case "auth/wrong-password":
this.statusMessage.message('Password you have enterd is wrong');
break;
default:
console.dir(error);
this.statusMessage.message('Something wen wrong! please try again later');
break;
}
}
注册码:
async register(form: NgForm) {
// checking for form validation
if (form.invalid) return this.statusMessage.message('Validation error!');
console.log(this.user);
try {
await this.afAuth.auth.createUserWithEmailAndPassword(this.user.email, this.user.password).then(
user => console.log(user)
);
} catch (error) {
this.statusMessage.message('Somthing went wrong!, please try again later.');
}
}
}
【问题讨论】:
-
请多一些代码
-
@ShashankVivek 你还需要什么?我用我也用过的注册方法更新了问题!
-
user.additionalUserInfo.isNewUser的响应总是在第一次登录后返回 false,这是我的问题! -
firebaser here 据我所知the
isNewUservalue comes straight from the server,所以应该不可能在那里得到错误的值。你能在像 JSBin 这样的网站上设置一个快速、最小化的重现(最好只使用 JavaScript SDK,不要使用其他框架),以便我可以与团队分享吗? -
@FrankvanPuffelen 据我了解,
createUserWithEmailAndPassword()方法在我调用它后也已登录用户!所以未来的签约不会再认为用户是新用户。这对我来说很奇怪!为了使用 isNewUser 我必须在注册后将用户重定向到他的个人资料!无需再次手动登录!
标签: typescript firebase ionic-framework firebase-authentication angularfire2