View as Json

Hybrid Auth MySQL (Drizzle)

Hybrid authentication with MySQL, Drizzle ORM, and Redis-backed sessions.

Installation Guide

Run the following command:

npx servercn-cli@latest add bp hybrid-auth

Add the following scripts to your package.json:

package.json
  "scripts": {
    "dev": "cross-env NODE_ENV=development npx tsx watch --clearScreen=false src/server.ts",
    "build": "rm -rf dist && tsc && tsc-alias",
    "start": "cross-env NODE_ENV=production node dist/server.js",
    "db:generate": "drizzle-kit generate",
    "db:migrate": "drizzle-kit migrate",
    "db:push": "drizzle-kit push",
    "db:studio": "drizzle-kit studio",
    "typecheck": "tsc --noEmit",
    "docs": "npx tsx swagger.config.ts",
    "prepare": "husky",
    "lint:check": "eslint .",
    "lint:fix": "eslint . --fix",
    "format:check": "npx prettier . --check",
    "format:fix": "npx prettier . --write"
  },

Key Features

  • MySQL + Drizzle ORM for type-safe database operations
  • JWT access/refresh token rotation with Redis-backed sessions
  • OAuth support (GitHub, Google) via Passport.js
  • OTP-based email verification and password reset
  • Account lockout after failed login attempts
  • Soft/hard account deletion with reactivation support
  • Cloudinary file upload integration for avatars
  • Rate limiting and security headers

API Endpoints

All endpoints are the same for both MVC and Feature architectures.

Base URL: http://localhost:9000/api/v1/auth

MethodEndpointDescription
POST/signupRegister new user and send verification OTP
POST/verify-userVerify email with OTP
POST/signinLogin user and set auth cookies
GET/profileGet current user profile
PATCH/profileUpdate profile and avatar
GET/sessionsList active sessions
DELETE/sessionsRevoke all sessions except current
DELETE/sessions/:sessionIdRevoke a specific session
POST/refresh-tokenRotate access and refresh tokens
POST/logoutLogout current session
POST/forgot-passwordRequest password reset OTP
POST/verify-reset-otpVerify password reset OTP
POST/reset-passwordReset password after OTP verification
POST/change-passwordChange password and revoke sessions
POST/account/request-deleteRequest account deletion token
DELETE/account/deleteDelete or deactivate account by token
PUT/account/reactivateReactivate soft-deleted account

Base URL: http://localhost:9000/api/auth

MethodEndpointDescription
GET/githubInitiate GitHub OAuth
GET/github/callbackGitHub OAuth callback
GET/googleInitiate Google OAuth
GET/google/callbackGoogle OAuth callback

Base URL: http://localhost:9000/api/v1/health

MethodEndpointDescription
GET/Basic health check
GET/detailedDetailed health status

Environment Variables

Create a .env file with the following:

PORT='9000'
NODE_ENV='development'
LOG_LEVEL='info'
CORS_ORIGIN=''
CLIENT_URL=''
 
DATABASE_URL='mySQL://user:password@localhost:5432/mydb'
REDIS_URL=''
 
JWT_ACCESS_SECRET=''
JWT_REFRESH_SECRET=''
CRYPTO_SECRET=''
 
RESEND_API_KEY=''
EMAIL_FROM=''
 
CLOUDINARY_CLOUD_NAME=''
CLOUDINARY_API_KEY=''
CLOUDINARY_API_SECRET=''
 
GOOGLE_CLIENT_ID=''
GOOGLE_CLIENT_SECRET=''
GOOGLE_REDIRECT_URI=''
 
GITHUB_CLIENT_ID=''
GITHUB_CLIENT_SECRET=''
GITHUB_REDIRECT_URI=''

File & Folder Structure

Loading files...

Installation

npx servercn-cli@latest add bp hybrid-auth

Contributed by