User
The user.controller.ts file manages user accounts, including registration, authentication, sign-out, and profile retrieval. These endpoints allow users to create accounts, log in, log out, and fetch their profile information.
Note: Some user routes use the
requireAuthmiddleware to ensure the user is authenticated. For more details, see the requireAuth documentation.
Key Functions
signUp
Registers a new user account.
Flow:
- Reads
usernameandpasswordfrom the request body. - Checks if the username already exists.
- Hashes the password with a randomly generated salt.
- Creates the user in the database.
- Generates JWT and refresh tokens, stores the refresh token, and sets both as httpOnly cookies.
- Returns a success response.
signIn
Authenticates a user and starts a session.
Flow:
- Reads
usernameandpasswordfrom the request body. - Verifies credentials by hashing and comparing the password.
- Generates JWT and refresh tokens, stores the refresh token, and sets both as httpOnly cookies.
- Returns a success response.
signOut
Logs out the authenticated user.
Flow:
- Clears the
tokenandrefreshTokencookies. - Removes the refresh token from the database.
- Returns a success response.
fetchUserById
Fetches the authenticated user's profile.
Flow:
- Uses the authenticated user's ID from the JWT payload.
- Retrieves the user's profile from the database (ID and username).
- Returns the user data in the response.
Routes
The following routes are defined in user.routes.ts and connect HTTP requests to the corresponding controller functions:
| Method | Path | Description | Controller Function | Middleware |
|---|---|---|---|---|
| POST | /api/user/signup | Register a new user | signUp | - |
| POST | /api/user/signin | Sign in and receive tokens | signIn | - |
| POST | /api/user/signout | Sign out user | signOut | requireAuth |
| GET | /api/user/fetchUserById | Get authenticated user's profile | fetchUserById | requireAuth |
Error Handling
- Returns
400 Bad Requestfor missing or invalid credentials. - Returns
500 Internal Server Errorfor unexpected errors.
Example Requests & Responses
Sign Up
Request:
POST /api/user/signup
Content-Type: application/json
{
"username": "player1",
"password": "securepassword"
}
Successful Response:
{
"success": true
}
Sign In
Request:
POST /api/user/signin
Content-Type: application/json
{
"username": "player1",
"password": "securepassword"
}
Successful Response:
{
"success": true
}
Fetch User By ID
Request:
GET /api/user/fetchUserById
Cookie: token=<JWT_TOKEN>
Successful Response:
{
"user": {
"id": "user123",
"username": "player1"
}
}
Sign Out
Request:
POST /api/user/signout
Cookie: token=<JWT_TOKEN>; refreshToken=<REFRESH_TOKEN>
Successful Response:
{
"success": true
}
Usage Instructions
- Register:
UsePOST /api/user/signupto create a new user account. - Sign In:
UsePOST /api/user/signinto authenticate and receive tokens. - Fetch Profile:
UseGET /api/user/fetchUserByIdto retrieve your profile (requires authentication). - Sign Out:
UsePOST /api/user/signoutto log out and clear session cookies.
Dependencies
- Express: Handles HTTP requests and responses.
- Prisma: ORM for database operations.
- Jsonwebtoken: For JWT token creation and verification.
- Crypto: For password hashing and token