NexGenStudioDev/FastKit

Build Reusable Auth Module with Plug-and-Play Signup/Login Support

Aperta

#1 aperta il 27 giu 2025

 (0 commenti) (0 reazioni) (0 assegnatari)TypeScript (0 fork)auto 404
TypeScriptbackenddocumentationenhancementgood first issuehelp wantednpm

Metriche repository

Star
 (1 stella)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

🧩 Description

Create a fully modular and reusable AuthController that handles signup and login out of the box. The goal is to allow any developer to import and use these methods in their own Express routes without writing controller logic manually.

🧱 Auth Module Tasks

This module provides authentication and authorization functionality using controllers, services, middlewares, validators, and constants. All tasks below have been completed.

✅ AuthController (Auth.controller.ts)

  • Created AuthController class.
  • Added method signupUser(req, res) to handle the signup flow.
  • Added method loginUser(req, res) to handle the login flow.
  • Parses and validates body input using validators.
  • Utilizes methods from AuthService.
  • Sends appropriate success or error responses.
  • Exported for direct use in route handlers.

✅ AuthService (Auth.service.ts)

  • signup():
    • Checks if the user already exists.
    • Hashes password.
    • Saves new user to the database.
  • login():
    • Validates if user exists and is not deleted.
    • Compares provided password with hashed password.
    • Returns a JWT token on successful login.
  • Uses constants and error messages defined in Auth.constant.ts.

✅ AuthValidators (Auth.validators.ts)

  • Uses Zod or Joi schema to validate:
    • Signup request payload.
    • Login request payload.

✅ AuthConstants (Auth.constant.ts)

  • Defines:
    • JWT secret key and expiration time.
    • Common authentication-related error messages.
    • Role definitions if applicable.

✅ AuthMiddleware (Auth.middleware.ts)

  • Middleware to:
    • Extract and verify JWT token from headers.
    • Attach decoded user info to req.user.

✅ AuthRoleMiddleware (AuthRole.middleware.ts)

  • Provides allowRoles(...roles):
    • Restricts route access based on user roles.
    • Checks req.user.role against allowed roles.

✅ AuthModel (Auth.model.ts)

  • Defines schema with:
    • email
    • password
    • isVerified
    • ImageUrl
    • isBlocked
    • isDeleted
    • socket_id
    • role_id
  • Supported roles:
    • admin, student, user

✅ Wrapper (Auth.ts)

  • Optional entry file to:
    • Expose controller, middleware, and service from a single module entry point for easier import.

✨ Goal

Allow anyone to use fastkit-auth with the following plug-and-play interface:

import { AuthController } from 'fastkit-auth';


router.post('/signup', AuthController.signupUser);
router.post('/login',  AuthController.loginUser);

📁 Target File Structure

features/
└── Auth/
└── v1/
├── Auth.controller.ts ✅ Create controller methods
├── Auth.service.ts ✅ Handle logic for signin/signup/login
├── Auth.validators.ts ✅ Validate input payloads
├── Auth.constant.ts ✅ Store constants including roles
├── Auth.middleware.ts ✅ verifyToken middleware (authentication)
├── AuthRole.middleware.ts ✅ allowRoles middleware (authorization by role)
├── Auth.model.ts ✅ User schema and role definitions
├── Auth.ts ✅ Optional wrapper exporting main modules

🙋🏻‍♂️ Looking For

  • Feedback on architecture
  • Suggestions for improvements
  • Contributors to help build role-based middleware
  • Ideas for multi-tenant or provider-based auth support

🎯 Goal

Allow developers to quickly use the Auth module in their Express app without writing signup/login logic manually.

@sanjaysah101

Guida contributor