For the complete documentation index, see llms.txt. This page is also available as Markdown.

JWT

JWT-based authentication

A React application with JWT-based authentication built with Vite, TypeScript, shadcn-ui, and Tailwind CSS.

Table of Contents

Quick Start

Prerequisites

  • Node.js (v16 or higher recommended)

  • npm (comes with Node.js)

Installation

  1. Clone the repository:

  1. Install dependencies:

  1. Start the development server:

  1. Access the application:

    • The app will be available at http://localhost:5173 (default Vite development server port)

    • Port 5173 is the default for Vite and can be customized in vite.config.ts or via --port flag

    • Important: You need a valid JWT token to access the application

JWT Authentication

This application uses JWT (JSON Web Token) authentication with RS256 algorithm. Users must provide a valid JWT token to access the application.

How Authentication Works

  1. Token-based Access: Users access the app by visiting: http://localhost:5173?token=YOUR_JWT_TOKEN

  2. Token Validation: The app validates the JWT using RSA public key cryptography

  3. Session Storage: Valid tokens are stored locally for subsequent visits

  4. Automatic Cleanup: The token parameter is removed from the URL after successful authentication

JWT Token Requirements

Your JWT token must:

  • Be signed with RS256 algorithm

  • Include the following claims:

    • sub (subject): User identifier

    • name: User's display name

    • email: User's email address

    • iat (issued at): Token creation timestamp

    • exp (expiry): Token expiration timestamp

Example JWT payload:

API Services

Text-to-Speech (TTS)

  • How it works: Text messages are sent to /api/tts/ endpoint with session ID and target language

  • What's sent: Response text, language preference (e.g., 'mr', 'hi', 'en'), and session identifier

  • Response: Base64-encoded audio data that gets played back to the user

Automatic Speech Recognition (ASR)

  • How it works: Audio is captured from user's microphone, converted to base64, and sent to /api/transcribe/

  • What's sent: Base64-encoded audio data along with the service identifier, and the session identifier

  • Response: Transcribed text and detected language code for further processing

All API calls require JWT authentication via Bearer token in headers.

Demo Mode

Quick Demo Access with jwt.io

🚀 Want to try the app immediately? Use jwt.io to generate a demo token:

  1. Go to jwt.io

  2. Set the Algorithm to RS256

  3. Use the demo public/private key pair:

Private Key (for signing):

Public Key (already configured in the app):

  1. Set the payload with your user data:

  1. Copy the generated JWT token from the left panel

  2. Visit your app with the token:

Basic JWT Signing Instructions for jwt.io:

  1. Paste your private key in the "Private Key" section (right panel)

  2. Verify the public key appears automatically in the "Public Key" section

  3. Edit the payload (middle section) with your desired claims

  4. The encoded JWT will appear in the left panel — copy this token

  5. Important: The signature will only be valid if the public/private key pair matches

Alternative Online JWT Tools

⚠️ IMPORTANT: The demo keys are for development only. DO NOT use them in production!

Production Setup

1. Generate Your Own RSA Key Pair

For production, you MUST generate your own RSA key pair using online tools:

Option 1: Using jwt.io

  1. Go to jwt.io

  2. Select RS256 algorithm

  3. Click "Generate New Keys" at the bottom

  4. Copy both the public and private keys

Option 2: Using Online Key Generators

Option 3: Using OpenSSL (if available)

2. Update the Public Key

Replace the publicKeyPEM in src/contexts/AuthContext.tsx with your production public key:

3. Secure Your Private Key

  • Store your private key securely (environment variables, key vault, etc.)

  • Never commit private keys to version control

  • Use the private key in your authentication service to sign JWT tokens

4. Configure Your Authentication Service

Your authentication service should:

  1. Validate user credentials (login, OAuth, etc.)

  2. Generate JWT tokens using your private key and any JWT library

  3. Redirect users to your app with the token: https://your-app.com?token=${token}

You can test token generation using jwt.io with your production keys before implementing in your auth service.

Development

File Structure

Key Files

  • src/contexts/AuthContext.tsx: Main authentication logic with demo public key

  • src/pages/ErrorPage.tsx: Handles authentication errors

Environment Variables

You can customize JWT settings and development server configuration via environment variables:

Testing Authentication

  1. Generate a test token using jwt.io:

    • Use RS256 algorithm

    • Use the demo private key provided above

    • Set your desired payload

  2. Test with the token:

  1. Verify authentication:

    • Check browser console for JWT validation logs

    • Confirm user data appears in the application

    • Test token persistence across page refreshes

Technologies Used

Security Considerations

Production Checklist

JWT Best Practices

  • Short expiration times: Use short-lived tokens (1–24 hours)

  • Refresh tokens: Implement refresh token rotation

  • Secure storage: Store tokens securely (httpOnly cookies preferred over localStorage)

  • Token validation: Always validate tokens server-side

  • Audience validation: Verify the aud claim matches your application

Troubleshooting

Common Issues

1. "Authentication Required" Error

  • Ensure you have a valid JWT token

  • Check token expiration

  • Verify the public key matches your private key

2. JWT Verification Failed

  • Confirm token is signed with RS256 algorithm

  • Check that public key in AuthContext.tsx is correct

  • Verify token hasn't expired

3. Token Not Persisting

  • Check browser's localStorage for token data

  • Ensure token includes required claims (sub, name, email)

Getting Help

  1. Check browser console for detailed error messages

  2. Verify JWT token at jwt.io

  3. Review authentication logs in browser developer tools

  4. Test with the demo keys provided in this README

Last updated