Overview
The Trippins API uses JWT (JSON Web Token) authentication to secure endpoints and verify user identity. Most API endpoints require a valid JWT token to access protected resources.JWT tokens are generated during login and must be included in the
Authorization header for all authenticated requests.How JWT Authentication Works
The authentication flow follows these steps:- User Login: Client sends credentials (email and password) to the login endpoint
- Token Generation: Server validates credentials and returns a JWT token with user roles
- Token Storage: Client stores the token securely (typically in memory or secure storage)
- Authenticated Requests: Client includes the token in the
Authorizationheader - Token Validation: Server validates the token and grants access if valid
Login Endpoint
POST /v1/api/login
Authenticates a user with their email and password, returning a JWT token and user roles.User’s email address
User’s password
Request Example
Success Response (200 OK)
The JWT access token to use for authenticated requests. This token is valid for 10 hours from the time of issuance.
Array of role strings assigned to the user. Common roles include
ROLE_USER and ROLE_ADMIN.Error Responses
401 Unauthorized - Invalid Credentials
401 Unauthorized - Invalid Credentials
400 Bad Request - Invalid Input
400 Bad Request - Invalid Input
Including JWT in Requests
Once you have obtained a JWT token, include it in subsequent API requests using one of two methods:Method 1: Authorization Header (Recommended)
Include the token in theAuthorization header with the Bearer scheme:
Method 2: Cookie-Based Authentication
The API also accepts JWT tokens via cookies. The cookie must be namedJWT:
The
Authorization header method is preferred for API clients. Cookie-based authentication is primarily used by the web application.JWT Token Structure
The JWT token contains encoded information about the authenticated user:Token Components
Decoded Payload
User’s assigned roles for authorization
Subject - the user’s email address
Issued At - Unix timestamp when token was created
Expiration - Unix timestamp when token expires
Token Expiration and Renewal
Token Lifetime
JWT tokens issued by the Trippins API are valid for 10 hours from the time of creation.Handling Expired Tokens
When a token expires, API requests will fail with a401 Unauthorized response. To handle this:
- Detect Expiration: Check for 401 responses
- Re-authenticate: Prompt the user to log in again
- Obtain New Token: Call
/v1/api/loginwith credentials
Token Validation Flow
The server validates tokens on every request using theJwtRequestFilter:
Role-Based Authorization
The Trippins API uses role-based access control (RBAC) to restrict access to certain endpoints.Available Roles
Standard user role - Can manage their own reservations, create reviews, and browse houses
Administrator role - Can accept/deny houses, manage all users, and moderate content
Admin-Only Endpoints
Certain endpoints require theROLE_ADMIN role:
Example: Admin Request
Security Best Practices
Store Tokens Securely
Store Tokens Securely
- Never store JWT tokens in localStorage (vulnerable to XSS attacks)
- Use httpOnly cookies or secure in-memory storage
- For mobile apps, use secure keychain/keystore
Use HTTPS Only
Use HTTPS Only
- Always use HTTPS to prevent token interception
- The API base URL uses
https://scheme - Never transmit tokens over unencrypted connections
Implement Token Expiration Handling
Implement Token Expiration Handling
- Monitor token expiration time
- Implement automatic re-authentication flow
- Handle 401 responses gracefully
Don't Share Tokens
Don't Share Tokens
Validate SSL Certificates
Validate SSL Certificates
- Ensure SSL certificate validation is enabled
- Don’t disable certificate verification in production
Authentication Implementation Example
Here’s a complete example of implementing authentication in a client application:Troubleshooting
401 Unauthorized - Bad credentials
401 Unauthorized - Bad credentials
401 Unauthorized - Token invalid
401 Unauthorized - Token invalid
403 Forbidden - Access denied
403 Forbidden - Access denied
Problem: Authenticated requests return 403 Forbidden.Solutions:
- Verify the user has the required role for the endpoint
- Admin endpoints require
ROLE_ADMINrole - Check that the user can access the requested resource
Token extracted is null
Token extracted is null
Problem: JWT filter cannot extract username from token.Solutions:
- Verify the token format is correct
- Check the Authorization header format:
Authorization: Bearer {token} - Ensure no extra spaces or characters in the header
- If using cookies, verify the cookie name is exactly “JWT”
Next Steps
User Management API
Learn how to create and manage user accounts
Housing API
Explore endpoints for managing hotel listings
Reservations API
Handle booking and reservation operations
Reviews API
Manage customer reviews and ratings
