How To Make A Login Page In Reails

How To Articles

Creating a login page is a crucial step in building any web application. It allows users to securely access their accounts and provides a personalized experience. In this article, I’ll guide you through the process of creating a login page in React using Rails as the backend.

Setting Up the Development Environment

Before we start building the login page, we need to make sure our development environment is properly set up. To do this, we need to have React and Rails installed on our system. If you don’t have them installed, follow the respective documentation and get them set up.

Creating a New Rails Application

First, let’s create a new Rails application. Open your terminal and run the following command:

rails new login-page

This will generate a new Rails application with the name “login-page”. Navigate into the project directory:

cd login-page

Setting Up the Backend

Next, we need to set up the backend of our application. Rails provides a convenient way to generate authentication code using the Devise gem. To add Devise to our Gemfile, open it in a code editor and add the following line:

gem 'devise'

Save the file and run the following command in the terminal to install the gem:

bundle install

After installing the Devise gem, we need to run the following command to generate the necessary configuration files:

rails generate devise:install

This command will generate a configuration file for Devise, which we can customize later. Next, we need to generate a User model for authentication:

rails generate devise User

This command creates a User model with the required fields for authentication.

Building the Login Page

Now that we have our backend set up, we can proceed to build the login page in React. In your project directory, run the following command to create a new React app:

npx create-react-app login-page-client

This command will generate a new React application in a folder named “login-page-client”. Navigate into the client directory:

cd login-page-client

Now, let’s install the necessary packages to connect our React app with the Rails backend. Run the following command:

yarn add axios react-router-dom

These packages will allow us to make API requests to the Rails backend and handle client-side routing.

Once the packages are installed, open the src/App.js file in a code editor. Import the necessary components:

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import LoginForm from './components/LoginForm';

Now, create a new component called App and define the routes for the login page:

function App() {
return (
<Route exact path="/" component={LoginForm} />

In the above code, we define a route for the login page and specify the LoginForm component to be rendered when users access the root path (“/”).

Now, let’s create the LoginForm component. Create a new folder called “components” inside the src directory and create a new file called LoginForm.js inside it.

In the LoginForm.js file, import the necessary components and define the LoginForm component:

import React, { useState } from 'react';
import axios from 'axios';

function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

const handleSubmit = async (e) => {
try {
const response = await'/users/sign_in', { email, password });
} catch (error) {

return (
<form onSubmit={handleSubmit}>
<input type="email" value={email} onChange={(e) => setEmail(} placeholder="Email" />
<input type="password" value={password} onChange={(e) => setPassword(} placeholder="Password" />
<button type="submit">Log In</button>

In the above code, we define two state variables to store the email and password entered by the user. The handleSubmit function is called when the form is submitted, sending a POST request to the Rails backend to authenticate the user.

Finally, open the src/index.js file and replace the existing ReactDOM.render method with the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

<App />


Congratulations! You have successfully created a login page in React using Rails as the backend. This article guided you through setting up the development environment, creating a new Rails application, setting up the backend with Devise, building the login page in React, and connecting it to the Rails backend.

Remember, the login page is a critical part of any web application, and it is essential to implement proper security measures to protect user data. Additionally, you can customize the login page’s appearance and add additional functionality, such as password recovery or social media login integration. Keep exploring and enhancing your login page to create a seamless user experience.

For more information and detailed documentation, be sure to visit the official websites of React and Rails.

Happy coding!