Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Detecting if user exists

How Can I Check user exists in Firebase auth in Signup Button via react native?

This is my Login Page Code:

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>
        )
    }
}
like image 792
Saeed Heidarizarei Avatar asked Jun 18 '17 14:06

Saeed Heidarizarei


People also ask

How do you check if a user has already exists in Firebase authentication?

If you want to check if a user already exists, then you have to save that information either in Firestore or in the Realtime Database, and then simply perform a query to check if a particular phone number already exists. Unfortunately, you cannot check that in the Firebase console.

How does Firebase identify a user?

Auth tokensCreated by Firebase when a user signs in to an app. These tokens are signed JWTs that securely identify a user in a Firebase project. These tokens contain basic profile information for a user, including the user's ID string, which is unique to the Firebase project.

What does Firebase auth () currentUser return?

console. log(firebase. auth(). currentUser) // This returns null console.


2 Answers

You need to use the fetchSignInMethodsForEmail API. It takes an email and returns a promise that resolves with the list of providers linked to that email if it is already registered: https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#fetchsigninmethodsforemail

like image 155
bojeil Avatar answered Sep 22 '22 03:09

bojeil


Here's how to use the fetchSignInMethodsForEmail API. It returns an array. If the array is empty, it means the user has never signed up/signed in to your app with the sign in methods you have used in your app.

This is an example with Google signin.

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.
        }
like image 28
Adebola Avatar answered Sep 21 '22 03:09

Adebola