Skip to content

Implement global auth state: AuthContext with useReducer, ProtectedRo…#187

Merged
Hexstar-labs merged 2 commits intoBrainTease:mainfrom
teslims2:global-auth-state
Mar 30, 2026
Merged

Implement global auth state: AuthContext with useReducer, ProtectedRo…#187
Hexstar-labs merged 2 commits intoBrainTease:mainfrom
teslims2:global-auth-state

Conversation

@teslims2
Copy link
Copy Markdown
Contributor

Global Authentication State Implementation

Description

This PR implements a global authentication state management system for the Brain-Storm application, ensuring that protected pages require user authentication.

Changes Made

  • Auth Context (lib/auth-context.tsx):

    • React Context + useReducer for state management
    • Manages user, token, and isLoading states
    • Decodes JWT token on app load and checks expiry
    • Provides useAuth() hook for accessing auth state
  • Protected Route Component (components/ProtectedRoute.tsx):

    • Redirects unauthenticated users to /auth/login
    • Shows loading state during authentication check
    • Wraps protected pages
  • Client Providers (components/ClientProviders.tsx):

    • Wraps the application with AuthProvider
    • Includes theme and auth providers
  • Layout Updates (app/[locale]/layout.tsx):

    • Integrated ClientProviders for global auth state
  • Protected Pages:

    • Dashboard (app/[locale]/page.tsx): Wrapped with ProtectedRoute
    • Courses (app/[locale]/courses/page.tsx): Wrapped with ProtectedRoute
    • Profile (app/[locale]/profile/page.tsx): Wrapped with ProtectedRoute

Features

  • JWT token validation and expiry checking
  • Automatic redirect for unauthenticated users
  • Loading states during auth initialization
  • Persistent auth state across page reloads
  • Clean separation of auth logic

Security

  • JWT tokens stored securely in localStorage
  • Automatic token expiry handling
  • Protected routes prevent unauthorized access

Testing

  • Auth state persists across page reloads
  • Unauthenticated users are redirected to login
  • JWT expiry is properly handled
  • Loading states work correctly

Screenshots

Related Issues

Checklist

  • Auth context with useReducer implemented
  • JWT decoding and expiry checking
  • ProtectedRoute component created
  • Protected pages wrapped
  • AuthProvider integrated in layout
  • useAuth hook exposed
  • Loading states handled
  • Redirect logic working

@Hexstar-labs Hexstar-labs merged commit b175ed9 into BrainTease:main Mar 30, 2026
3 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontend: Auth — implement auth context and protected routes

2 participants