Overview
The Juadah API uses JWT (JSON Web Token) based authentication with a dual-token system. Authentication tokens are stored in HTTP-only cookies for enhanced security.Key Features
- JWT-based authentication with access and refresh tokens
- Cookie-based token storage with HTTP-only and secure flags
- Email verification via OTP (One-Time Password)
- Role-based access control (Admin and User roles)
- Automatic token refresh for seamless user experience
Token System
The API uses two types of tokens:Access Token
Short-lived token (10 minutes) used for API requests
Refresh Token
Long-lived token (10 days) used to obtain new access tokens
Token Storage
Both tokens are stored as HTTP-only cookies:- Access Token: Available on all paths with
HttpOnly,Secure, andSameSite=noneflags - Refresh Token: Restricted to
/api/refreshpath with the same security flags
Registration Flow
Create a new user account with email verification.Receive Tokens
On successful registration, you receive:
- User data in the response body
- Access token in
Set-Cookieheader - Refresh token in
Set-Cookieheader - OTP sent to email for verification
Registration Validation
The API validates the following fields:| Field | Requirements |
|---|---|
fullname | Minimum 1 character |
email | Valid email format, must be unique |
password | Minimum 1 character |
passwordConfirmation | Must match password |
New users are assigned the
USER role by default. Admin accounts must be created through a different process.Login Flow
Authenticate existing users and obtain access tokens.Verify Credentials
The API performs the following checks:
- Validates email format
- Retrieves user from database
- Compares password using bcrypt (salt rounds: 10)
- Generates new access and refresh tokens
src/auth/service.ts:104-175Token Refresh Flow
Access tokens expire after 10 minutes. Use the refresh token to obtain a new access token without requiring the user to log in again.Detect Token Expiration
When you receive a 401 Unauthorized response, it’s time to refresh the access token.
Validation Process
The API validates the refresh token:
- Verifies JWT signature and expiration
- Extracts user data from token payload
- Compares token with stored value in database
- Generates new access token
src/auth/service.ts:177-231The refresh endpoint returns both the user data and sets a new access token cookie. The refresh token remains the same.
Role-Based Access Control
The API supports two user roles with different permissions:User Roles
- USER
- ADMIN
Standard user role assigned during registration.Permissions:
- View products
- Create orders
- View own orders
- Update own profile
Role in JWT Payload
The user’s role is encoded in both access and refresh tokens:Security Best Practices
Cookie Security
Cookie Security
Password Hashing
Password Hashing
Passwords are hashed using bcrypt with 10 salt rounds before storage.
Token Verification
Token Verification
All tokens are signed using HMAC-SHA256 algorithm and verified on each request.
Token Storage in Database
Token Storage in Database
Refresh tokens are stored in the database and validated against the stored value to prevent token reuse attacks.
Code Examples
Troubleshooting
Invalid credentials error
Invalid credentials error
Error:
email or password is incorrectSolutions:- Verify email format is correct
- Check password matches the registered password
- Ensure account exists (try registration if new user)
Refresh token unavailable
Refresh token unavailable
Invalid refresh token
Invalid refresh token
Error:
invalid refresh tokenSolutions:- Token may have expired (10 day maximum)
- Token may have been invalidated (user logged out)
- User needs to log in again to get a new refresh token
Email already exists
Email already exists
Error:
this email already existsSolutions:- Use a different email address
- If you own this account, use the login endpoint instead
- Contact support if you believe this is an error
Next Steps
Error Handling
Learn how to handle API errors gracefully
Products API
Start working with the Products API
Rate Limits
Understand API usage limits and best practices
Payments
Implement payment processing