Overview
TerraQuake API uses JWT (JSON Web Token) authentication for protected endpoints. Most endpoints are public and don’t require authentication, making it easy to get started immediately.Public endpoints like
/earthquakes/* and /stations/* don’t require authentication. You can start querying earthquake data right away!Public vs Protected Endpoints
Public Endpoints (No Authentication Required)
These endpoints are accessible without any authentication:- All Earthquake Endpoints:
/earthquakes/recent,/earthquakes/today,/earthquakes/magnitude, etc. - All Station Endpoints:
/stations,/stations/code,/stations/geojson, etc. - Most Utility Endpoints: Statistics, FAQs, documentation, etc.
Protected Endpoints (Authentication Required)
These endpoints require a valid JWT token:- User Profile:
GET /auth/me- Get current user information - Logout:
POST /auth/logout- Invalidate current session - Change Password:
POST /auth/change-password- Update user password - Admin Endpoints: All endpoints under
/admin/*(requires admin role)
Getting Started with Authentication
Create an Account
Register for an account using the signup endpoint:
You’ll receive a confirmation email after registration. Check your inbox to verify your account.
Token Validation
When you make a request to a protected endpoint, the API validates your token through these checks:- Token Presence: The
Authorizationheader must include a valid token - Token Format: Must be in the format
Bearer <token> - Signature Verification: Token signature is verified using the secret key
- Expiration Check: Token must not be expired
- Blacklist Check: Token must not be revoked (e.g., after logout)
- User Validation: User account must exist and be active
Token validation is handled by the authentication middleware at
/home/daytona/workspace/source/backend/src/middleware/authMiddleware.js:11Error Responses
The API returns specific error messages for authentication failures:Missing Token
Invalid or Expired Token
User Not Found
Malformed Token
Password Management
Change Password
Update your password while authenticated:cURL
Forgot Password
Request a password reset link:cURL
Reset Password
Reset your password using the token from the email:cURL
Logout
Invalidate your current session token:cURL
OAuth Authentication
TerraQuake API also supports OAuth authentication with Google:Google Sign-In Flow
- Redirect to Google: Direct users to
/auth/google - Handle Callback: After user grants permission, Google redirects to
/auth/google/callback - Receive Token: The API returns a JWT token for the authenticated user
Example: Google OAuth
Best Practices
Secure Storage
Store tokens securely in environment variables or secure storage (e.g., httpOnly cookies)
Token Refresh
Sign in again when tokens expire rather than storing long-lived tokens
HTTPS Only
Always use HTTPS when transmitting tokens to prevent interception
Logout Properly
Always call the logout endpoint when users sign out to invalidate tokens
Next Steps
Error Handling
Learn how to handle authentication errors gracefully
API Reference
Explore all available endpoints