-
Notifications
You must be signed in to change notification settings - Fork 0
Authentication API Specification
/auth
| Field | Type | Constraints |
|---|---|---|
id |
UUID | Primary Key |
firstName |
varchar2 | Not Null |
lastName |
varchar2 | |
email |
varchar2 | Not Null, Unique |
emailVerified |
bool | Default: false
|
mobile |
varchar2 | Not Null, Unique |
mobileVerified |
bool | Default: false
|
googleId |
varchar2 | Unique |
role |
enum | Default: USER (Roles like ADMIN will be added later) |
lastLogout |
timestamp | Timestamp when the user last logged out |
createdAt |
timestamp | Auto-generated |
updatedAt |
timestamp | Auto-updated |
| Field | Type | Constraints |
|---|---|---|
id |
UUID | Primary Key |
userId |
UUID | Foreign Key (Users.id) |
hashedPassword |
varchar2 |
This API follows JWT-based access token and refresh token authentication. Both token types serve different purposes:
- Access Token: Short-lived, used for authenticating user actions.
- Refresh Token: Long-lived, used to obtain new access tokens.
Token Payload (for both tokens):
{
"userId": "string",
"userEmail": "string",
"firstName": "string",
"lastName": "string"
}Note: User roles will be added to the token payload in future iterations. Token utility functions are kept separate to allow easy changes in payload without affecting core API logic.
Description: Registers a new user using email and password. The password is hashed before storing it in the database.
Request Body:
{
"firstName": "string",
"lastName": "string",
"email": "string",
"mobile": "string",
"password": "string"
}Important Instructions:
-
Password Hashing: Before storing the user’s password, hash the password using a secure algorithm such as
bcrypt. - Store the hashed password in the
HashedPasswordtable linked to the user.
Response Codes:
- 201 Created: On successful signup.
- 400 Bad Request: On validation errors or if email/mobile already exists.
Description: Authenticates a user using email and password. Provides both access and refresh tokens upon successful login.
Request Body:
{
"email": "string",
"password": "string"
}Process:
- Validate the email and password.
- Compare the provided password with the stored hashed password using a secure comparison method.
- Generate access and refresh tokens with the payload:
userId,userEmail,firstName,lastName.
Response Codes:
- 200 OK: On successful login.
- 401 Unauthorized: If the credentials are incorrect.
Description: Verifies user details via Google OAuth. Adds a new user to the database or updates their googleId if they already exist.
Request Body:
{
"googleToken": "string"
}Process:
- Verify the
googleTokenwith Google’s OAuth service. - If the user does not exist, create a new user record with the provided details.
- If the user already exists, update their
googleId.
Response Codes:
- 200 OK: On successful sign-in.
- 400 Bad Request: If the Google token is invalid.
Description: Refreshes the access token using the refresh token provided by the user.
Request Body:
{
"refreshToken": "string"
}Process:
- Validate the refresh token.
- If valid, generate a new access token with the same payload structure:
userId,userEmail,firstName,lastName.
Response Codes:
- 200 OK: On successful token refresh.
- 401 Unauthorized: If the refresh token is invalid or expired.
Description: Logs out the authenticated user by updating the lastLogout timestamp, invalidating all tokens issued before that time.
Request Headers:
Authorization: Bearer access_token_jwt
Process:
- Ensure the user is authenticated using the
Authorizationheader. - Update the
lastLogouttimestamp for the authenticated user to invalidate any tokens issued before this time.
Response Codes:
- 200 OK: On successful logout.
- 401 Unauthorized: If the user is not authenticated or the access token is invalid.
-
Password Hashing: Use a secure hashing algorithm like
bcryptto hash the user's password before storing it in the database. - Access Token Expiry: Access tokens should be short-lived (15-30 minutes).
- Refresh Token Expiry: Refresh tokens should be long-lived (e.g., 7 days) but stored securely (e.g., HttpOnly cookies or secure storage).
- HTTPS: Enforce HTTPS for all API calls to protect tokens in transit.
-
Token Revocation: Use the
lastLogouttimestamp in theUserstable to invalidate tokens issued before the logout time. - Strong Passwords: Enforce strong password rules (e.g., minimum length, character variety).
-
Rate Limiting: Apply rate limiting to sensitive endpoints like
/auth/loginand/auth/signupto prevent brute-force attacks.
-
Token Generation: Implement token generation functions separately to allow for easy modifications to the token payload (e.g., adding roles in future iterations).
- Function to generate access and refresh tokens based on user details.
- Token Validation: Separate utility functions for token validation, so changes in token structure (like adding roles) won’t affect the rest of the API.
- Payload Flexibility: Keep the payload structure isolated in the token utility layer to ensure smooth transitions if new fields (such as roles) are added later.
-
Role-Based Access Control (RBAC): In future iterations, introduce roles (
USER,ADMIN, etc.) to the token payload and implement RBAC to restrict access to certain endpoints based on user roles. - Data Validation: Ensure robust validation for all input fields to prevent SQL injection or similar attacks.
- Error Handling: Provide consistent error handling across the API, returning descriptive messages for invalid tokens, missing fields, etc.