0

I'm developing an application using react-native. I can easily connect my react-native application to firebase. My idea is to use firebase authentication, but with data from my MySQL database. What is the best/correct way to use firebase and mysql?

My idea is use ajax request from react-native to mysql in order to validate the username and password against the data into my MySQL database. Then use the ID returned from this request to create or load a user from firebase. Is it the correct way?

I am sorry if it doe snot make sense. I just start working with react-native and firebase.

Thanks

2
  • You would have to have a dedicated server for that, otherwise itd be insecure. Commented Dec 14, 2018 at 15:58
  • Do you mean a server for my MySQL database? Commented Dec 14, 2018 at 16:08

1 Answer 1

3

Well...

For mysql you can use axios plugin. Is the best way to work with mysql database.

Firebase use asynchronous request, if you want work with both the best way is using axios.

First, you get user from your mysql table, correct? So.. you do something like that :

return axios.get(server_address&param=PARAM_VALUE)                          
            .then(response => {                                     
                return(response);
            }).catch(function(error) {
                alert.error(error.message);
            });

Axios aways return a JSON response. You can use GET or POST method.

So... with the JSON, you can send for firebase your data for load or create user.

like that:

return firebase
    .auth()
    .signInWithEmailAndPassword(loginEmail,loginPassword)
    .then( user => {            
        return user;
    })
    .catch(error => {
        if ((error.code == 'auth/user-not-found') || (error.code == 'auth/invalid-email'))  {
            return new Promise((resolve, reject) => {
                Alert.alert(
                    'User not found',
                    'Create ?',
                    [{
                        text: 'No',
                        onPress:() => resolve(),
                        style:'cancel'
                    },{
                        text:'Yes',
                        onPress: () =>{
                            firebase
                            .auth()
                            .createUserWithEmailAndPassword(loginEmail,loginPassword)
                            .then(resolve)
                            .catch(reject)
                        }
                    }],
                    {cancelable: false}
                )
            })
        }
        return Promise.reject(error)
    })

For a complete guide to axios : https://github.com/qiangmao/axios#readme

For a complete guide to firebase: https://firebase.google.com/docs/auth/?hl=en

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.