A complete guide to implementing JWT-based registration, login, session persistence, and token refresh using the MonkeysLegion v2 API with a Next.js (or React) frontend.
Table of Contents
API Endpoints Overview
Backend Setup (MonkeysLegion)
Frontend: API Client
Frontend: Auth Context Provider
Frontend: Register Page
Frontend: Login Page
Frontend: Protected Routes
Token Refresh Flow
Common Issues & Solutions
API Endpoints Overview
MonkeysLegion v2 exposes the following auth endpoints under the /api/v2/auth prefix:
Method Endpoint Auth Description
POST /auth/register No Create a new user + company
POST /auth/login No Authenticate and get JWT tokens
POST /auth/refresh No Exchange refresh token for new access token
GET /auth/me Yes Get authenticated user profile
POST /auth/logout Yes Invalidate tokens
POST /auth/forgot-password No Request password reset
Response Shapes
Login Response — POST /auth/login
{ "data" : { "access_token" : "eyJ0eXAiOiJKV1Q..." , "refresh_token" :…