Javascript Firebase检测用户是否存在
我如何通过react native在Javascript Firebase检测用户是否存在,javascript,firebase,react-native,firebase-authentication,Javascript,Firebase,React Native,Firebase Authentication,我如何通过react native在注册按钮中检查Firebase auth中是否存在用户 这是我的登录页面代码: export default class Login extends Component { constructor(props) { super(props) this.state = { email: '', password: '', response: ''
注册按钮中检查Firebase auth中是否存在用户
这是我的登录页面代码:
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>
)
}
}
导出默认类登录扩展组件{
建造师(道具){
超级(道具)
此.state={
电子邮件:“”,
密码:“”,
响应:“”
}
this.signUp=this.signUp.bind(this)
this.login=this.login.bind(this)
}
异步注册(){
试一试{
等待firebase.auth().createUserWithEmailAndPassword(this.state.email,this.state.password)
这是我的国家({
响应:“已创建帐户!”
})
设置超时(()=>{
这个是.props.navigator.push({
id:'应用程序'
})
}, 500)
}捕获(错误){
这是我的国家({
响应:error.toString()
})
}
}
异步登录(){
试一试{
等待firebase.auth().signInWithEmailAndPassword(this.state.email,this.state.password)
这是我的国家({
响应:“用户登录”
})
设置超时(()=>{
这个是.props.navigator.push({
id:'应用程序'
})
})
}捕获(错误){
这是我的国家({
响应:error.toString()
})
}
}
render(){
返回(
this.setState({email})}
/>
this.setState({password})}
/>
登录
报名
)
}
}
您需要使用fetchSigningMethodsForemail API。它接收一封电子邮件并返回一个承诺,该承诺与链接到该电子邮件的提供商列表(如果已注册)达成一致:
这很简单。
在注册函数中将then()和catch()添加到firebase方法中
firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
.then(()=>{
console.log('Signup successful.');
this.setState({
response: 'Account Created!'
})
})
.catch((error)=> {
console.log(error.code);
console.log(error.message);
});
关于不同错误代码的信息:
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>
)
}
}
- 身份验证/电子邮件已在使用中
如果已存在具有给定电子邮件地址的帐户,则引发
- 验证/无效电子邮件
如果电子邮件地址无效,则引发
- 不允许进行身份验证/操作
如果未启用电子邮件/密码帐户,则引发。在Firebase控制台的“身份验证”选项卡下启用电子邮件/密码帐户
- 验证/弱密码
如果密码不够强,则抛出
以下是如何使用
fetchSigningMethodsForemail
API。它返回一个数组。如果数组为空,则表示用户从未使用您在应用中使用的登录方法注册/登录您的应用
这是谷歌登录的一个例子
firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
let token = result.credential.accessToken;
let user = result.user;
firebase
.auth()
.fetchSignInMethodsForEmail(user.email)
.then((result) => {
console.log('result', result);
});
})
.catch((error) => {
// Handle Errors here.
}
对于我来说,fetchSigningMethodForemail()
仅适用于使用电子邮件/密码注册的电子邮件,而不适用于使用苹果、LinkedIn或其他提供商注册的电子邮件
为了解决这个问题,我提出了以下解决方法:
我相信有一个适当的解决办法,我会更新这个答案,当我找到它
希望这将有助于某人你想只使用一个按钮注册和登录吗?如果是这样,您可以使用Email和Password进行登录,如果错误显示该用户不存在,您可以提供注册。处理Catch上的异常我有两个按钮,一个登录按钮和一个注册按钮,但我希望当用户想要注册时告诉用户此电子邮件已被接收,或者不要只需调用createUserWithEmailAndPassword
并查找已在使用的auth/email
错误代码-请参阅。谢谢,我是firebase的新手,你能更新我的代码吗?谢谢。我是firebase的新手,我应该在哪里添加代码?你能告诉我一个例子吗?你首先要求用户提供电子邮件,你调用这个API,如果数组是空的,显示注册页面,在那里你调用createUserWithEmailAndPassword。否则,如果返回的数组包含字符串“password”,则显示登录屏幕并要求用户提供密码。使用Apple登录而不提供电子邮件怎么样?你如何检查那种类型的账户?(在iOS平台上必须登录苹果)此解决方案的问题是,如果没有登录,将创建该帐户。如果您希望在电子邮件已被使用的情况下向用户提供输入模糊的即时反馈,则这些功能将不起作用。auth/email已被使用
如果您拥有FB帐户并使用Apple帐户创建新登录,则不会触发。Facebook帐户将被删除或链接(如果其电子邮件已被验证),根据:signInWithPopup(提供商)将在“then”中创建用户so,响应始终为“google.com”