React Native User Login Using PHP MySQL Android iOS Example Tutorial

As we have discussed in our previous tutorial about User Registration with PHP MySQL. This tutorial is the second part of that tutorial in this tutorial we would going to create a react native application that gives us the functionality to Login the already registered user and after successfully login it will redirect us to Profile page. So let’s get started .

What we are doing in this project ( Project Description ) :

In this project we are going to create a react native application with 2 Activities. First is LoginActivity, which shows us the user Login form containing 2 Text Input and 1 Button component. Now when user clicks on the Login button then we start a web call on server using fetch method, which send the User Email and Password on server. Now we would receive the email and password on server using PHP script, after that we would match the Email and Password using SQL command and on success of that match we will print a response message using Echo. Now we would receive that response in our react native application.

After receiving that response if the response == Data Matched then we would open a new activity named as Profile activity and send the user email to Profile activity. If you have any query regarding this tutorial feel free to comment .

Database used in this tutorial : MySQL

Server Side scripting Language used in this tutorial : PHP

Contents in this project User Login Using PHP MySQL tutorial :

1. Read my previous tutorial User Registration with PHP MySQL . I have explained all about creating MySQL database in that tutorial.

2. Create PHP Script to Match received Email and Password from App into MySQL database and send response to application :

Create 2 PHP files DBConfig.php and User_Login.php .

DBConfig.php : This file contains the MySQL database hostname, database name, database password and database username.

User_Login.php : This file would receive the user email and password and match them into MySQL database using SQL command.

Code for DBConfig.php file :

Code for User_Login.php file :

3. Start a fresh React Native project. If you don’t know how then read my this tutorial.

4. We are using 2 Activities in our project, First one is LoginActivity and Second is ProfileActivity. So read my this tutorial to learn about Adding Activities in our react native project.

5. Now add AppRegistry, StyleSheet, TextInput, View, Alert, Button and Text component in import block.

Add stack Navigator import line.

6. Create a new activity class named as LoginActivity.

7. Add static navigation options inside LoginActivity. Using the static navigationOptions we can set the activity title name which displays inside the Action bar / Title bar.

8. Creating constructor with props in class.

9. Create UserLoginFunction() in LoginActivity .

In this function we would first get the Email and Password from Text Input using state. Now we would send that email password to server and receive them using PHP script we have created earlier in this project.

10. Add render’s return block in your LoginActivity and inside that First set a parent View, Inside that parent view place 2 TextInput and 1 Button component. Calling the LoginFunction on button onPress.

Screenshot of LoginActivity :


11. Now Add a New activity named as ProfileActivity in your project.

12. Add static navigation options inside ProfileActivity . Using the static navigationOptions we can set the activity title name which displays inside the Action bar / Title bar.

13. Create render’s return block in ProfileActivity. Now create const {goBack} = this.props.navigation method inside the render block. This will help us to enable activity closing using button click. Add a View as parent and inside that View add 1 Text and 1 Button component.

Text component : is used to show the user email entered by user at login time on LoginActivity.

Button : is used to Logout from Profile Activity and after that it will automatically redirect to Login Activity.

Screenshot of Profile Activity :

14. Navigate the LoginActivity and ProfileActivity using StackNavigator method. Place this code just after the ProfileActivity class.

First object navigate to LoginActivity.

Second object navigate to ProfileActivity.

15. Now add CSS styles :

16. Complete source code for App.js file.

Screenshot in Android Device :

Login

Screenshot in iOS device :