You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This document explains how the authentication system works in our TypeScript Express API, covering user registration and login using Prisma (PostgreSQL), JWT, and bcrypt.
🔹 Overview
This module provides authentication functionalities:
User Registration (/register)
User Login (/login)
JWT Token-Based Authentication
Secure Password Hashing
Manual Input Validation
🔹 Dependencies Used
Package
Purpose
express
Web framework for Node.js
bcryptjs
Hashing passwords securely
jsonwebtoken
Generating & verifying JWT tokens
@prisma/client
ORM for PostgreSQL database
cookie-parser
Storing JWT in secure cookies (optional)
🔹 Environment Variables
Variable
Description
JWT_SECRET
Secret key for signing JWT tokens
DATABASE_URL
PostgreSQL connection string
📌 Ensure you define these in your .env file before running the server.
exportconstregister=async(req: Request,res: Response,next: NextFunction)=>{try{const{ name, email, password }=req.body;// Manual validationif(!name||name.length<2){returnres.status(400).json({message: "Name must be at least 2 characters"});}if(!email||!email.includes("@")||!email.includes(".")){returnres.status(400).json({message: "Invalid email format"});}if(!password||password.length<6){returnres.status(400).json({message: "Password must be at least 6 characters"});}// Hash password before storing in DBconsthashedPassword=awaitbcrypt.hash(password,10);// Create user in PostgreSQL using Prismaconstuser=awaitprisma.user.create({data: { name, email,password: hashedPassword},});res.status(201).json({message: "User registered successfully",userId: user.id});}catch(error){console.error("Error registering user:",error);res.status(500).json({message: "Internal Server Error"});}};
✅ What You Mastered
TypeScript Express Backend
JWT Authentication & Secure Cookies
User Registration, Login, and Deletion
Redux Toolkit for Authentication Management
Fetching and Managing Users in React
Debugging CORS & API Route Issues
Proper Backend & Frontend Integration