signin

Creating a Blogging App Using React, Part 1: User Sign-In

In this tutorial series, you’ll see how to get started with creating a blogging app using React. Throughout the course of this tutorial series, you’ll be focusing on how to use React for developing the application user interface. You’ll be using Node.js for the server side of the application.

In this tutorial, you’ll see how to implement the user interface and back end for user registration and user sign-in.

Getting Started

Create a project directory called ReactNodeApp. Navigate to the project directory and initiate the node project.

Fill in the required details and you should have the package.json file created. Here is how it looks:

You’ll be using the express framework for serving your application. Install express using the following command:

Using the express framework, let’s create our application listening on a port address. Inside the project directory, create a file called app.js. Require the express module inside the app.js and create an app. Set the static path of the application where it can find the static files. Here is how it looks:

Assign a port number for the application to listen on a port. Add the following code to create a server:

Inside the project directory, create a folder called html. Inside the html folder, create a file called index.html. Add the following code to index.html:

Save the above changes and start the server using the following command:

Point your browser to http://localhost:7777/index.html and you should be able to see the index.html page.

Creating the Sign-In View

You’ll be using bootstrap to create the user interface. Download and include bootstrap in the index.html page.

Add the required React libraries in the index.html page.

You’ll be creating the view using JSX. If you are not familiar with JSX, I would recommend reading an introductory tutorial on React and JSX.

To transform JSX code to JavaScript, you’ll need babel, a JavaScript compiler. Include babel in the index.html page.

Create a file called main.jsx inside the html folder. This file will contain the React UI components. 

Let’s create a new React component called Signin inside the main.jsx file. 

Add a render method inside the Signin component which will display the UI for our Signin component.

In the above code, it’s all HTML with just one difference. The class attribute has been modified to className when used in JSX.

The Signin component, when displayed, will display the HTML code inside the render method. 

Add a container div in the index.html page where you’ll render the Signin component.

Render the Signin component inside the .container div in the index.html

Save the above changes and restart the node server. Point your browser to http://localhost:7777/index.html and you should be able to view the sign-in screen.

React Blog App SignIn Screen

Implementing User Sign-In  

To implement the sign-in process, you need to handle the input text onChange event and keep the text box values in a state variable. When the user clicks the button, you’ll make use of the state variables to read the email address and password text box values. So, let’s add the onChange event to the text boxes:

Define the onChange events in the Signin component:

Bind the above defined events and the state variables in the component constructor method:

Define an onClick method which you’ll invoke on button click.

Add the OnClick event to the SignIn button.

Save the above changes and restart the node server. Point your browser to http://localhost:7777/index.html. Enter the email address and password and click the Sign In button, and you should be able to see the email and password pop up.

Posting Data From React to the Node Service 

Once you have the data on the client side, you need to post that data to the Node.js server method to validate the user sign-in. For posting the data, you’ll make use of another script called axios. Axios is a promise-based HTTP client for the browser and Node.js. Include axios in the index.html page.

Inside the signin method in the main.jsx file, add the following line of code to make a post request.

The code makes a post request to the /signin method with the parameters shown. Once the request is successful, the promise is resolved in the then callback. On error, the response is logged in the catch callback. 

Let’s create a signin method on the Node.js side to validate the user sign-in process. In the app.js file, create a method called signin.

You’ll be making use of the body-parser module to parse the request posted from the React client side. Install the body-parser module in the project. 

Require the body-parser module in the app.js file.

Add the following line of code to enable JSON parsing.

Inside the signin method, you can parse the request as shown:

Modify the signin method as shown to validate the user sign-in.

For the time being, the user credentials have been hard-coded. You can replace this with the appropriate service as per your preference.

Once the parameters have been parsed, they are validated against the expected credentials. If true, a success message is passed, else a failure message is returned.

Save the above changes and restart the Node.js server. Enter a valid username and password and click the sign-in method. Based on the credentials, it will return a success or failure message, which will be displayed in the browser console.

Creating the User Registration View

The process of creating the user registration view is quite similar to how you implemented the user sign-in module. Let’s start by creating the Signup component in the main.jsx file.

Since sign-up and sign-in are two different components, you need to link the two components. For the purpose of routing, you’ll be making use of react-router. If you are new to routing in React, I would recommend reading the React routing tutorial.

Include react-router in the index.html page.

Define the required react-router variables to create links in the main.jsx file.

Define the different application routes and the default route as shown below:

Include a link to the sign-in component in the sign-up component and vice versa. Here is the Signin component’s render method with the sign-up link:

Save the above changes and restart the Node.js server. Point your browser to http://localhost:7777/index.html and you should be able to see the sign-in screen with the sign-up link. Click on the sign-up link and you should be navigated to the sign-up screen.

Sign In Screen with Sign Up Link

Implementing user sign-up is similar to how you implemented user sign-in. I’ll leave the user sign-up implementation as an exercise. I’ll post the user sign-up implementation in the next part of this tutorial series.

Wrapping It Up

In this part of the tutorial series, you created and implemented the Sign In screen. You also saw how to use react-router to implement routing in React. In the next part of this tutorial, you’ll see how to implement the Sign Up portion and the Add Post page.

Source code from this tutorial is available on GitHub.

Do let us know your thoughts and suggestions in the comments below.

Leave a Comment

Scroll to Top