Secure, modern full-stack JWT authentication starter.
Build and ship production-style auth flows fast: Signup, Login, Protected Routes, Persistent Sessions, and polished UI.
Auth Token Flow is a complete reference implementation for authentication systems in modern web apps.
It includes a secure Express API and a React Router frontend with clean UX, route protection, and session persistence.
Perfect for:
- learning auth architecture end-to-end
- bootstrapping SaaS/app starters
- interview-ready project demos
|
Signup/Login endpoints with validation, duplicate checks, bcrypt hashing, and JWT generation. |
Backend middleware verifies Bearer token and blocks unauthorized profile access. |
Styled Home/Login/Signup/Dashboard pages with Auth Context, loading states, and clear errors. |
| Capability | Details |
|---|---|
| Password Security | bcrypt hashing only, never plain text storage |
| Token Auth | JWT with expiresIn: 1d |
| Session Persistence | Token stored in localStorage, restored by Auth Context |
| Protected API | GET /api/profile requires Authorization: Bearer <token> |
| Error Handling | Duplicate email, invalid credentials, missing token all handled cleanly |
Four simple steps from account creation to protected access:
- Signup — user submits
name,email,password - Login — backend validates credentials and returns JWT
- Persist Session — frontend stores token and restores auth state on reload
- Access Protected Data — dashboard fetches
/api/profilewith Bearer token
auth-token-flow/
├─ backend/
│ ├─ config/db.js
│ ├─ middleware/authMiddleware.js
│ ├─ models/User.js
│ ├─ public/index.html
│ ├─ routes/authRoutes.js
│ ├─ routes/profileRoutes.js
│ └─ server.js
├─ frontend/
│ ├─ app/context/AuthContext.tsx
│ ├─ app/lib/api.ts
│ ├─ app/lib/http.ts
│ ├─ app/lib/token.ts
│ ├─ app/routes/
│ │ ├─ home.tsx
│ │ ├─ login.tsx
│ │ ├─ signup.tsx
│ │ └─ dashboard.tsx
│ └─ app/routes.ts
└─ public/images/
├─ backend.png
└─ fronted.png
PORT=3000
MONGODB_URI=your_mongodb_connection_string
JWT_SECRET=your_super_secret_keyVITE_API_URL=http://localhost:3000/api/auth- Node.js
18+ - npm
- MongoDB Atlas (or local MongoDB)
cd backend
npm install
npm run devBackend runs at: http://localhost:3000
cd frontend
npm install
npm run devFrontend runs at: http://localhost:5173
- Validates
name,email,password - Normalizes email
- Checks duplicate users
- Hashes password and creates account
- Verifies email/password
- Returns JWT (
expiresIn: 1d)
- Requires
Authorization: Bearer <token> - Verifies JWT via middleware
- Returns authenticated user profile
- Passwords are never stored in plain text
- JWT secret is loaded from
.env - Token expiry enabled (
1d) - Protected route blocks unauthenticated requests
- Login errors are generic to avoid user enumeration
- Signup success
- Duplicate email returns error
- Login success
- Wrong password returns error
- Protected route works with token
- Protected route fails without token

