User Login With WordPress using React Native

Date Posted: July 14, 2017, 4:08 pm


After seeing a lot of requests about this topic on Google, I’ve decided to make my own tutorial about it. So let’s get started!

On the render()  method of your React Native component, create a Form with input fields for Username and Password.

Then on form submit, the data gets POSTed to a WordPress API server where we check if credentials are correct and throw an error if incorrect, if the credentials are correct – we generate a token which is just a combination of strings and numbers (your choice). This token will then be stored in the wp_usermeta table, which you can use later on to do checks every time a user will access your mobile app.

Login to your server via your favorite FTP program and then from the root directory of WordPress, create a PHP file: “authentication.php” then add the following code:

So now that we have a token, we pass it back to our mobile app as a response. Our mobile app should then be able to receive and store the token (and any other data you want to include) into our mobile device storage via AsyncStorage , this way, every time the user opens your mobile app, our application will just check the storage if it has a currently logged-in user (Persistent Login).

The saveToStorage() method:

Full Code:

Then we can provide a logout button which basically clears the storage and notifies the server to clear the token associated with the currently logged-in user.

Here is how you would implement the logout method:

 

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me