Skip to main content

Overview

The Zarna backend uses JWT (JSON Web Tokens) for authentication with middleware that protects all API routes. Middleware Location: api/app/middleware/JWTAuthMiddleware

JWT Authentication Middleware

Implementation

Middleware Registration

Using Authentication in Routes

Accessing User Info

Dependency Injection (Alternative)

Token Generation

Login Endpoint

Token Refresh

Security Best Practices

  • Access tokens: 24 hours (balances security and UX)
  • Refresh tokens: 7 days
  • Session tokens: 30 days for “remember me”
Tokens are sensitive and must be transmitted over HTTPS only.
Issue new token on each refresh and invalidate old ones.

Error Handling

Custom Exception Handlers

Configuration

Environment Variables

Middleware Order

Testing

Test with cURL

Test with Python

Troubleshooting

Causes:
  • Token expired (> 24 hours)
  • Invalid token signature
  • Missing Authorization header
  • Incorrect token format
Solution:
  • Check token expiration
  • Verify SUPABASE_JWT_SECRET matches
  • Ensure header format: Authorization: Bearer {token}
  • Try refreshing token
Cause: Origin not allowedSolution:

Next Steps

Authentication Flow

Complete authentication patterns

API Reference

API documentation

OAuth Setup

Third-party OAuth

Security Guide

Security best practices