Authentication API
The Authentication API provides secure user authentication, email verification, password management, and token handling for your applications.All endpoints use the base URL:
https://jethings-backend.fly.dev (production) or http://localhost:3000 (development)🔐 Authentication Flow
1
Sign Up
Create account with email verification
2
Verify Email
Enter OTP sent to email
3
Sign In
Get access and refresh tokens
4
Make Requests
Use access token in Authorization header
5
Refresh Token
Get new tokens when access token expires
📝 User Registration
Sign Up
Create a new user account with email verification.| Field | Type | Required | Description |
|---|---|---|---|
firstName | string | ✅ | User’s first name |
lastName | string | ✅ | User’s last name |
email | string | ✅ | Valid email address (must be unique) |
phoneNumber | string | ✅ | Valid phone number (must be unique) |
password | string | ✅ | Minimum 6 characters |
age | number | ✅ | Integer between 1-120 |
description | string | ❌ | Optional user bio |
409 Conflict: Email already exists409 Conflict: Phone number already exists400 Bad Request: Validation errors
Verify Email
Verify user email with the OTP sent during signup.| Field | Type | Required | Description |
|---|---|---|---|
otp | string | ✅ | 6-digit verification code |
400 Bad Request: Invalid or expired OTP400 Bad Request: Too many attempts (max 3)
- OTP expires after 10 minutes
- Maximum 3 attempts per OTP
- OTP is automatically deleted after successful verification
🔑 User Authentication
Sign In
Authenticate user and receive access/refresh tokens.| Field | Type | Required | Description |
|---|---|---|---|
email | string | ✅ | User’s email address |
password | string | ✅ | User’s password |
401 Unauthorized: Invalid credentials401 Unauthorized: Email not verified
- Access token expires in 15 minutes
- Refresh token expires in 7 days
- Store refresh token securely (httpOnly cookie recommended)
Get Current User
Retrieve current authenticated user’s profile.401 Unauthorized: Invalid or expired token401 Unauthorized: User not found401 Unauthorized: Account deactivated
🔄 Refresh Token Flow
Overview
Jethings Backend implements a secure refresh token authentication system that provides persistent, secure user sessions without compromising security. Token Types:- Access Token: Short-lived JWT (15 minutes) used to authenticate API requests
- Refresh Token: Long-lived token (7 days) used to obtain new access tokens
- Automatic token refresh via backend middleware
- Manual token refresh option
- Token rotation on each refresh
- Secure token revocation
Authentication Flow Diagram
Storing Tokens
Store tokens securely on the client. Common approaches: Web Applications:localStorage- Persists across browser sessionssessionStorage- Persists only for current sessionhttpOnlycookies (recommended for refresh tokens)- React Context/State Management
- Secure storage (Keychain on iOS, Keystore on Android)
- Encrypted shared preferences
Option 1: Automatic Token Refresh (Recommended)
The backend middleware automatically refreshes expired access tokens when both tokens are included in the request headers. This is the simplest and recommended approach. How it works:- Include both
Authorization: Bearer <accessToken>andx-refresh-token: <refreshToken>headers - If the access token is expired, the backend automatically refreshes it
- New tokens are returned in response headers
- Update your stored tokens with the new values
| Header | Description |
|---|---|
x-new-access-token | New access token (if refreshed) |
x-new-refresh-token | New refresh token (if refreshed) |
x-token-refreshed | "true" if tokens were refreshed |
x-token-expires-in | Token expiration time in seconds |
Option 2: Manual Token Refresh
Implement your own refresh logic before making requests. This gives you more control over when tokens are refreshed. Implementation Example:React Hook Example
For React applications, here’s a custom hook for managing authentication:Axios Interceptor Example
For applications using Axios, you can use interceptors to handle token refresh automatically:Sign Out / Token Revocation
Always revoke refresh tokens when the user signs out:Security Best Practices
- ✅ Never expose refresh tokens in URLs, client-side logs, or error messages
- ✅ Use HTTPS in production to protect tokens in transit
- ✅ Consider httpOnly cookies for refresh tokens in web applications
- ✅ Implement token rotation - always use new refresh tokens when received
- ✅ Handle token expiration gracefully - redirect to login when refresh fails
- ✅ Store tokens securely - use secure storage mechanisms appropriate for your platform
- ✅ Revoke tokens on logout - always call the revoke endpoint when users sign out
- ✅ Clear tokens on errors - remove stored tokens if refresh fails
🔄 Token Management
Refresh Token
Get new access and refresh tokens when the current access token expires.| Field | Type | Required | Description |
|---|---|---|---|
refreshToken | string | ✅ | Valid refresh token |
401 Unauthorized: Invalid or expired refresh token
Revoke Token
Revoke a specific refresh token (logout from one device).Revoke All Tokens
Revoke all refresh tokens for the current user (logout from all devices).Logout
Alias for revoke-all-tokens (logout from all devices).🔒 Password Management
Request Password Reset
Request a password reset OTP to be sent to user’s email.| Field | Type | Required | Description |
|---|---|---|---|
email | string | ✅ | User’s email address |
Always returns this message to prevent email enumeration attacks.
Verify Password Reset
Reset password using the OTP sent to user’s email.| Field | Type | Required | Description |
|---|---|---|---|
otp | string | ✅ | 6-digit reset code |
newPassword | string | ✅ | New password (min 6 characters) |
400 Bad Request: Invalid or expired OTP400 Bad Request: Too many attempts (max 3)
🛠️ Development Endpoints
Create Super Admin (Development Only)
Create a super admin account for development purposes.| Field | Type | Required | Description |
|---|---|---|---|
firstName | string | ✅ | Super admin’s first name |
lastName | string | ✅ | Super admin’s last name |
email | string | ✅ | Valid email address (must be unique) |
password | string | ✅ | Minimum 6 characters |
phoneNumber | string | ✅ | Valid phone number (must be unique) |
age | number | ✅ | Integer between 1-120 |
description | string | ❌ | Optional description |
403 Forbidden: Endpoint only available in development409 Conflict: Email already exists409 Conflict: Phone number already exists
🔧 Error Handling
Common Error Codes
| Status Code | Description | Common Causes |
|---|---|---|
400 Bad Request | Invalid request data | Validation errors, malformed JSON |
401 Unauthorized | Authentication required | Missing/invalid token, expired token |
403 Forbidden | Insufficient permissions | Wrong role, development-only endpoint |
404 Not Found | Resource not found | Invalid endpoint, missing resource |
409 Conflict | Resource conflict | Email/phone already exists |
500 Internal Server Error | Server error | Database issues, unexpected errors |