Skip to content

An easy to start auth template for express js projects easy to setup complete authentication system also includes social logins using passport and swagger documentation as well

Notifications You must be signed in to change notification settings

FarazAhmad-117/create-express-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Auth System Starter with Passport.js, JWT, Role-Based Access, and Social Logins

Welcome to the Auth System Starter—an open-source, feature-rich authentication boilerplate built with Node.js, Express.js, and MongoDB. This project supports multiple authentication methods (including manual signup/login with JWT and social logins using Passport.js), role-based access control, email verification, and more.

This repository is a perfect starting point for developers looking to implement authentication in their applications quickly. Feel free to contribute, fine-tune, and customize as per your needs!


Features

  • 🌐 Authentication Options:
    • Manual login/signup with JWT.
    • Social logins with Google, Facebook, GitHub, and Apple ID.
  • 🛡 Role-Based Access Control (RBAC):
    • Create and manage custom roles.
    • Assign multiple roles to users.
  • 📧 Email Verification:
    • Secure email verification using NodeMailer.
  • 🔐 Secure Password Storage:
    • Hashing with bcrypt.
  • 🛠 Modular Project Structure:
    • Controllers for business logic, helpers for utilities.
  • 📖 Swagger Documentation:
    • API routes fully documented for easy integration.
  • 🚀 Scalable and Open Source:
    • Designed for extensibility and contributions from the community.

Project Structure

Here’s an organized project structure to help you navigate the codebase:

📂 auth-system-starter
├── 📁 config
│   └── db.js                   # MongoDB connection setup
│   └── passport.js             # Passport strategies configuration
├── 📁 controllers
│   └── authController.js       # Handles authentication logic
│   └── userController.js       # Manages user-related actions
├── 📁 helpers
│   └── sendEmail.js            # Email utility for sending emails
├── 📁 middlewares
│   └── authMiddleware.js       # Middleware for role-based access and JWT validation
├── 📁 models
│   └── User.js                 # Mongoose schema for User
│   └── Role.js                 # Mongoose schema for Role
├── 📁 routes
│   └── authRoutes.js           # Routes for authentication and authorization
│   └── userRoutes.js           # Routes for user management
├── 📁 utils
│   └── swagger.js              # Swagger setup for API documentation
├── .env.example                        # Environment variables
├── README.md                   # Project documentation
├── package.json                # Project dependencies and scripts
└── index.js                   # Entry point of the application

Getting Started

Follow these steps to get this project up and running:

1. Create Project

npx create-express-auth-starter

2. Install Dependencies

Install using your preferred package manager:

npm install
# or
yarn install
# or
pnpm install

3. Set Environment Variables

Create a .env file in the project root and add the following:

PORT=5000
MONGO_URI=your-mongodb-connection-string
JWT_SECRET=your-jwt-secret
EMAIL_USER=[email protected]
EMAIL_PASSWORD=your-email-password
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
FACEBOOK_CLIENT_ID=your-facebook-client-id
FACEBOOK_CLIENT_SECRET=your-facebook-client-secret
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
APPLE_CLIENT_ID=your-apple-client-id
APPLE_CLIENT_SECRET=your-apple-client-secret
CLIENT_URL=http://localhost:3000 # Replace with your frontend URL

4. Get Google ENVs

  • Go to https://console.cloud.google.com/apis/dashboard
  • Click on create New Project Image
  • Give a name Image
  • Go to the project's dashboard using this dropdown Image
  • Go to Library Section and search for Google People API and enable it Image
  • Go to the credentials and click on create credentials Image
  • Click on OAuth Client ID and then on Configure Consent Screen Image
  • Click on External and then create Image
  • Configure your app Image
  • Once set Now go to the same Create OAuth Client ID Again this time you will be able to create the application Image
  • Add the redirect url as given Image
  • Now Copy and Paste Image
  • You can hit the google auth by GET Request over http://localhost:5000/api/auth/google

5. Get Github ENVs

6. Get Facebook ENVs

7. Start the Server in development mode

npm run dev
# or
yarn dev
# or
pnpm run dev

The server will start on http://localhost:5000.


Swagger API Documentation

The API documentation is available at:

http://localhost:5000/api-docs

Swagger is pre-configured to provide an interactive interface for testing and understanding the API endpoints.


Contributing

We welcome contributions to improve this project! Here’s how you can help:

  1. Fork this repository.
  2. Create a feature branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature").
  4. Push to the branch (git push origin feature-name).
  5. Open a pull request.

Publishing as an NPM, PNPM, or Yarn Package

To make this project available to the public as a package:

1. Prepare for Publishing

  • Ensure package.json has the relevant metadata:
    {
      "name": "auth-system-starter",
      "version": "1.0.0",
      "description": "A starter project for authentication with Passport.js, JWT, and role-based access control.",
      "main": "server.js",
      "keywords": ["authentication", "passport.js", "jwt", "nodejs", "express"],
      "author": "Faraz Ahmad <[email protected]>",
      "license": "MIT"
    }

License

This project is licensed under the MIT License. Feel free to use, modify, and distribute it.


Community

  • 🌟 Star this repository to show your support.
  • 💬 Join discussions and share your ideas or issues.
  • 🛠 Contribute to make it even better.

Let’s build amazing applications together! 🚀

About

An easy to start auth template for express js projects easy to setup complete authentication system also includes social logins using passport and swagger documentation as well

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published