【发布时间】:2017-11-20 19:10:29
【问题描述】:
如何通过本机反应检查 Signup 按钮中的 Firebase 身份验证中的用户是否存在?
这是我的登录页面代码:
export default class Login extends Component {
constructor(props) {
super(props)
this.state = {
email: '',
password: '',
response: ''
}
this.signUp = this.signUp.bind(this)
this.login = this.login.bind(this)
}
async signUp() {
try {
await firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
this.setState({
response: 'Account Created!'
})
setTimeout(() => {
this.props.navigator.push({
id: 'App'
})
}, 500)
} catch (error) {
this.setState({
response: error.toString()
})
}
}
async login() {
try {
await firebase.auth().signInWithEmailAndPassword(this.state.email, this.state.password)
this.setState({
response: 'user login in'
})
setTimeout(() => {
this.props.navigator.push({
id: 'App'
})
})
} catch (error) {
this.setState({
response: error.toString()
})
}
}
render() {
return (
<View style={styles.container}>
<View style={styles.containerInputes}>
<TextInput
placeholderTextColor="gray"
placeholder="Email"
style={styles.inputText}
onChangeText={(email) => this.setState({ email })}
/>
<TextInput
placeholderTextColor="gray"
placeholder="Password"
style={styles.inputText}
password={true}
secureTextEntry={true}
onChangeText={(password) => this.setState({ password })}
/>
</View>
<TouchableHighlight
onPress={this.login}
style={[styles.loginButton, styles.button]}
>
<Text
style={styles.textButton}
>Login</Text>
</TouchableHighlight>
<TouchableHighlight
onPress={this.signUp}
style={[styles.loginButton, styles.button]}
>
<Text
style={styles.textButton}
>Signup</Text>
</TouchableHighlight>
</View>
)
}
}
【问题讨论】:
-
您想只使用一个按钮进行注册和登录吗?如果是这样,您可以执行 signInWithEmailAndPassword 并且如果错误显示用户不存在,您可以提供注册。处理捕获的异常
-
我有 2 个按钮,一个登录按钮和一个注册按钮,但我想当用户想要注册时告诉用户这封电子邮件是否被占用
-
只需调用
createUserWithEmailAndPassword并查找auth/email-already-in-use错误代码 - 请参阅 docs。 -
谢谢,我是 firebase 新手,你能更新我的代码吗?
标签: javascript firebase react-native firebase-authentication