The LMS Backend uses JWT tokens delivered as HTTP-only cookies. When you sign in, the server mints a signed token, writes it into aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Pragyat-Nikunj/Learning-Management-System-backend/llms.txt
Use this file to discover all available pages before exploring further.
Set-Cookie header, and every subsequent request to a protected endpoint is authenticated by reading that cookie — no Authorization header required. This approach keeps the token out of JavaScript’s reach on the client side.
How the token is issued
When a sign-in or sign-up request succeeds,generateToken signs a JWT containing the user’s _id and sets it in a cookie:
utils/generateToken.js
httpOnly flag prevents client-side JavaScript from reading the cookie. The sameSite flag provides CSRF protection.
Signing in
Send credentials
POST your email and password to the sign-in endpoint. The server fetches the user record (including the hashed password via
select('+password')), compares the password with bcrypt, and returns the token cookie on success.Making authenticated requests
Protected endpoints read the token fromreq.cookies.token. Pass the saved cookie jar on every subsequent call:
Cookie header:
How the server verifies the token
TheisAuthenticated middleware runs on every protected route:
middleware/auth.middleware.js
req.id is set to the authenticated user’s MongoDB _id. Controllers use req.id to scope database queries to the current user.
401 error responses
You will receive a401 in two situations:
No token cookie present
No token cookie present
Token is invalid or tampered
Token is invalid or tampered
The cookie is present but
jwt.verify rejects it — the token has been modified, was signed with a different secret, or the server’s SECRET_KEY changed.Invalid email or password on sign-in
Invalid email or password on sign-in
The credentials supplied to
/api/v1/user/signin do not match a stored user record. The response is intentionally generic to avoid leaking whether the email exists.Signing out
POST to/api/v1/user/signout. The server overwrites the token cookie with maxAge: 0, which instructs the client to delete it immediately:
Password reset flow
If a user forgets their password, use the two-step reset flow — no authentication cookie is required for either step.Request a reset token
POST the user’s email address. The server generates a random 20-byte token with
crypto.randomBytes, stores its SHA-256 hash on the user record, and sets a 10-minute expiry. The raw (unhashed) token is returned in the response.