【发布时间】:2020-07-25 19:56:40
【问题描述】:
我的Vue 应用中有一个注册页面。在此页面中,我接受用户输入并在验证后将用户添加到firebase auth。如果这成功,我希望页面导航到登录页面。她是我的注册页面:
<template>
<div class="signup container">
<form @submit.prevent="signup" class="card-panel">
<h2 class="center teal-text">Signup</h2>
<div class="field">
<label for="firstName">First Name:</label>
<input type="text" name="firstName" v-model="firstName">
</div>
<p class="red-text center" v-if="firstNameFeedback"> {{firstNameFeedback}} </p>
<div class="field">
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" v-model="lastName">
</div>
<p class="red-text center" v-if="lastNameFeedback"> {{lastNameFeedback}} </p>
<div class="field">
<label for="phone">Phone number:</label>
<input type="tel" name="phone" v-model="phone">
</div>
<p class="red-text center" v-if="phoneFeedback"> {{phoneFeedback}} </p>
<div class="field">
<label for="email">Email:</label>
<input type="email" name="email" v-model="email">
</div>
<p class="red-text center" v-if="emailFeedback"> {{emailFeedback}} </p>
<div class="field">
<label for="password">Password:</label>
<input type="password" name="password" v-model="password">
</div>
<div class="field">
<label for="repassword">Retype Password:</label>
<input type="password" name="repassword" v-model="rePassword">
</div>
<p class="red-text center" v-if="rePasswordFeedback"> {{rePasswordFeedback}} </p>
<div class="field center">
<button class="btn deep-teal">Signup</button>
</div>
</form>
</div>
</template>
<script>
import db from '@/firebase/init'
import firebase from 'firebase'
export default {
name: 'Signup',
data(){
return{
firstName: null,
firstNameFeedback: null,
lastName: null,
lastNameFeedback: null,
phone: null,
phoneFeedback: null,
email: null,
password: null,
rePassword: null,
emailFeedback: null,
passwordFeedback: null,
rePasswordFeedback: null
}
},
methods:{
signup(){
if (this.firstName == null) {
this.firstNameFeedback = "First name is required"
}
if (this.lastName == null) {
this.lastNameFeedback = "Last name is required"
}
if (this.phone == null) {
this.phoneFeedback = "Phone number is required"
}
if (this.email == null) {
this.emailFeedback = "Email is required"
}
if (this.password == null) {
this.passwordFeedback = "Password is required"
}
if (this.password != this.rePassword) {
this.rePasswordFeedback = "Passwords do not match"
}
if (this.firstName && this.lastName && this.email && this.phone && this.password && this.rePassword && this.password == this.rePassword) {
this.firstNameFeedback = null
this.lastNameFeedback = null
this.emailFeedback = null
this.phoneFeedback = null
this.passwordFeedback = null
this.rePasswordFeedback = null
firebase.auth().createUserWithEmailAndPassword(this.email, this.password).then(
cred => {
db.collection('admin').doc(cred.user.uid).set({
admin: this.$route.params.admin,
email: this.email,
fname: this.firstName,
phone: this.phone,
surname: this.lastName,
'user.uid': cred.user.uid
})
}
).catch(
err => {
console.log(err.message)
this.rePasswordFeedback = err.message
}
).then(
this.$router.push({name: 'Login'})
)
}
}
}
}
</script>
<style>
.signup{
max-width: 600px;
margin-top: 60px;
}
.signup h2{
font-size: 2.4em;
}
.signup .field{
margin-bottom: 16px;
}
</style>
正如您在代码中看到的,我在 catch 块中捕获了 createUserWithEmailAndPassword() 方法的错误。但是页面首先路由到登录页面并执行捕获。我知道这一点,因为在应用程序进入登录页面后错误消息被记录到控制台。任何帮助将不胜感激。谢谢
【问题讨论】:
-
如果您的代码触发了
catch()块,则意味着调用createUserWithEmailAndPassword()或set()方法时出错。console.log(err.message)的输出是什么? -
@RenaudTarnec 它说有一个用户使用这个电子邮件地址。是的,我知道有错误,但如果有错误,我不想执行 then 方法
标签: javascript firebase vue.js firebase-authentication vue-router