Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Complete Authentication with JWT

Overview

This repository provides a comprehensive implementation of user authentication using JSON Web Tokens (JWT) in a MERN (MongoDB, Express, React, Node.js) stack application. The project is designed to guide developers in creating secure, token-based authentication systems for their web applications. It encompasses user registration, login, protected routes, and token management, demonstrating best practices in web security.

Features

  • User Registration: Users can create an account by providing necessary details, which are validated and stored in a MongoDB database.
  • User Login: Secure login mechanism that generates JWT tokens upon successful authentication.
  • Token Management: Implementation of JWT for secure communication between client and server, including token expiration and refresh.
  • Protected Routes: Middleware to protect certain routes and ensure that only authenticated users can access them.
  • Error Handling: Comprehensive error handling to provide meaningful feedback during authentication processes.
  • Environment Configuration: Easy configuration for different environments using environment variables.

Tech Stack

  • Frontend: React
  • Backend: Node.js with Express
  • Database: MongoDB
  • Authentication: JSON Web Tokens (JWT)
  • Middleware: Express middleware for route protection
  • Development Tools: npm, Postman for API testing

Installation

  1. Clone the Repository:

    git clone https://github.com/yourusername/Complete-Authentication-with-JWT.git
    cd Complete-Authentication-with-JWT
  2. Install Dependencies:

    • Navigate to both the client and server directories and install the necessary packages:
    cd client
    npm install
    
    cd ../server
    npm install
  3. Set Up Environment Variables:

    • Create a .env file in the server directory and add the following variables:
    PORT=5000
    MONGO_URI=your_mongo_database_uri
    JWT_SECRET=your_jwt_secret_key
  4. Run the Application:

    • Start the server:
    cd server
    npm start
    • Start the client:
    cd client
    npm start

Usage

  • Register: Send a POST request to /api/auth/register with the user details.
  • Login: Send a POST request to /api/auth/login with credentials to receive a JWT.
  • Access Protected Routes: Include the JWT in the Authorization header as a Bearer token when accessing protected routes.

API Documentation

The API endpoints are structured as follows:

  • POST /api/auth/register: Register a new user
  • POST /api/auth/login: Authenticate user and return JWT
  • GET /api/protected: Example protected route requiring a valid JWT

Contributing

Contributions are welcome! If you have suggestions or improvements, please fork the repository and create a pull request.

About

This project implements a robust authentication system using JSON Web Tokens (JWT) for secure user authentication in a web application. Built with Node.js and Express.js, the application allows users to register, log in, and access protected routes using JWTs for session management.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages