Building an Advanced Login System with Node.js from Scratch

A login system is a fundamental component of many web applications. It ensures that users can securely access their accounts and personal data. In this guide, we'll delve deep into creating an advanced login system using Node.js, enhancing the user experience and security.

sequenceDiagram User->>Frontend: Enter details Frontend->>Backend: Send details Backend->>Database: Check user Database-->>Backend: User data Backend-->>Frontend: Authentication status Frontend-->>User: Display status

Prerequisites

Before diving in, ensure you have the following:

  • Node.js and npm installed on your local machine.
  • A MySQL server up and running.
  • Basic understanding of Express.js and MySQL.

Setting Up the Node.js Application

Initializing the Project

First, create a new directory for your project:

Bash
mkdir advanced-login-system && cd advanced-login-system

Initialize a new Node.js project:

Bash
npm init -y

Installing Necessary Libraries

Install the required libraries:

Bash
npm install express mysql dotenv hbs bcryptjs express-session

Here's a brief overview of each library:

  • Express.js: Framework for building the backend.
  • MySQL: To connect with our MySQL database.
  • dotenv: Manage environment variables.
  • hbs: Handlebars templating engine.
  • bcryptjs: Securely hash passwords.
  • express-session: Manage user sessions.

Setting Up the Database

Create a new database named advanced_login_db. Then, construct a users table with fields: ID, name, email, password, and registration_date.

Ensure your database connection details are stored in a .env file:

JavaScript
DATABASE=advanced_login_db
DATABASE_HOST=localhost
DATABASE_USER=root
DATABASE_PASSWORD=your_password

Designing the Login and Registration Forms

Registration Form

The registration form will collect the user's name, email, and password. Additionally, we'll include a date picker for the user to select their date of birth, enhancing the user experience.

JavaScript
<form action="/auth/register" method="POST">
    <div class="mb-3">
        <label for="name" class="form-label">Name</label>
        <input type="text" class="form-control" id="name" name="name" required>
    </div>
    <div class="mb-3">
        <label for="email" class="form-label">Email</label>
        <input type="email" class="form-control" id="email" name="email" required>
    </div>
    <div class="mb-3">
        <label for="password" class="form-label">Password</label>
        <input type="password" class="form-control" id="password" name="password" required>
    </div>
    <div class="mb-3">
        <label for="dob" class="form-label">Date of Birth</label>
        <input type="date" class="form-control" id="dob" name="dob">
    </div>
    <button type="submit" class="btn btn-primary">Register</button>
</form>

Login Form

The login form will be straightforward, requiring only the user's email and password.

JavaScript
<form action="/auth/login" method="POST">
    <div class="mb-3">
        <label for="email" class="form-label">Email</label>
        <input type="email" class="form-control" id="email" name="email" required>
    </div>
    <div class="mb-3">
        <label for="password" class="form-label">Password</label>
        <input type="password" class="form-control" id="password" name="password" required>
    </div>
    <button type="submit" class="btn btn-primary">Login</button>
</form>

Backend Logic

User Registration

When a user submits the registration form, the backend will:

  1. Check if the email already exists in the database.
  2. Hash the user's password using bcryptjs.
  3. Store the user's details in the database.

User Login

For user login:

  1. Verify if the email exists in the database.
  2. Compare the hashed password in the database with the entered password.
  3. If successful, initiate a user session.

Enhancing Security with Two-Factor Authentication (2FA)

To further secure user accounts, integrate 2FA. After entering the correct email and password, the user will receive a one-time code on their registered email. Only upon entering this code will they be granted access.

FAQs

Q: How secure is this login system?
A: This system uses bcryptjs for password hashing and offers 2FA, making it highly secure.

Q: Can I integrate third-party logins?
A: Yes, you can integrate OAuth for logins via platforms like Google or Facebook.

Q: How is the user session managed?
A: We use the express-session library to manage user sessions securely.

Author