Firebase检测用户是否存在

Sed*_*rei 5 javascript firebase react-native firebase-authentication

如何Signup通过反应原生来检查按钮中的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>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

boj*_*eil 13

您需要使用fetchProvidersForEmail API.它会收到一封电子邮件并返回一份承诺,该承诺会在已注册的情况下与链接到该电子邮件的提供商列表一起解决:https: //firebase.google.com/docs/reference/js/firebase.auth.Auth#fetchProvidersForEmail

  • 您首先会要求用户提供电子邮件,您调用此 API,如果数组为空,则显示您调用 createUserWithEmailAndPassword 的注册页面。否则,如果返回的数组包含字符串“password”,则显示登录屏幕并要求用户提供密码。 (2认同)

小智 5

下面介绍如何使用fetchSignInMethodsForEmailAPI。它返回一个数组。如果数组为空,则表示用户从未使用您在应用中使用的登录方法注册/登录您的应用。

这是 Google 登录的示例。

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.
        }
Run Code Online (Sandbox Code Playgroud)

  • signInWithPopup(provider) 将创建用户,因此在“then”中响应始终为“google.com” (2认同)