How it works
- The client sends credentials (email + password) to
/api/auth/signupor/api/auth/login. - The server hashes the password with bcrypt (10 salt rounds) and verifies it against the stored hash.
- On success, the server signs a JWT containing
userId,email, andname, with a 30-day expiry. - The client stores the token and sends it as a
Bearertoken in theAuthorizationheader on every subsequent request. authMiddlewareintercepts incoming requests, splits theBearer <token>string, and callsauthService.validateToken()to verify the signature and expiry.- If valid,
req.useris populated with the decoded payload and the request proceeds to the controller.
Signup
POST/api/auth/signup
Create a new account. Returns a token immediately — no separate login step required.
Request body
Example
Response — 201 Created
Login
POST/api/auth/login
Authenticate an existing user. Returns a fresh token.
Request body
Example
Response — 200 OK
Using the token
Include the token in theAuthorization header for every protected request:
End-to-end example
Sign up to get a token, then use it to fetch your profile:Protected vs public endpoints
Only the two auth routes are public. Every other endpoint requires a valid Bearer token:The auth middleware
authMiddleware (defined in src/middlewares/auth.ts) runs on every protected route. It reads the Authorization header, splits the Bearer <token> string, and calls authService.validateToken() to verify the JWT signature and expiry. If validation passes, it attaches { userId, email, name } to req.user so controllers can access the authenticated user’s identity without querying the database again.