mirror of
https://github.com/aljazceru/enclava.git
synced 2025-12-17 07:24:34 +01:00
auth consolidation
This commit is contained in:
@@ -4,7 +4,7 @@ import './globals.css'
|
||||
import { ThemeProvider } from '@/components/providers/theme-provider'
|
||||
import { Toaster } from '@/components/ui/toaster'
|
||||
import { Toaster as HotToaster } from 'react-hot-toast'
|
||||
import { AuthProvider } from '@/contexts/AuthContext'
|
||||
import { AuthProvider } from '@/components/providers/auth-provider'
|
||||
import { ModulesProvider } from '@/contexts/ModulesContext'
|
||||
import { PluginProvider } from '@/contexts/PluginContext'
|
||||
import { ToastProvider } from '@/contexts/ToastContext'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useAuth } from "@/contexts/AuthContext"
|
||||
import { useAuth } from "@/components/providers/auth-provider"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useEffect } from "react"
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useEffect, useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useAuth } from "@/contexts/AuthContext"
|
||||
import { useAuth } from "@/components/providers/auth-provider"
|
||||
|
||||
interface ProtectedRouteProps {
|
||||
children: React.ReactNode
|
||||
|
||||
@@ -8,7 +8,7 @@ import { Button } from "@/components/ui/button"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { ThemeToggle } from "@/components/ui/theme-toggle"
|
||||
import { UserMenu } from "@/components/ui/user-menu"
|
||||
import { useAuth } from "@/contexts/AuthContext"
|
||||
import { useAuth } from "@/components/providers/auth-provider"
|
||||
import { useModules } from "@/contexts/ModulesContext"
|
||||
import { usePlugin } from "@/contexts/PluginContext"
|
||||
import {
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Badge } from "@/components/ui/badge"
|
||||
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { useAuth } from "@/contexts/AuthContext"
|
||||
import { useAuth } from "@/components/providers/auth-provider"
|
||||
import { useToast } from "@/hooks/use-toast"
|
||||
import {
|
||||
DropdownMenu,
|
||||
|
||||
@@ -114,9 +114,17 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('user', JSON.stringify(user))
|
||||
}
|
||||
} else if (response.status === 401) {
|
||||
// Token is invalid or expired, clear it
|
||||
console.log('Token invalid, clearing tokens')
|
||||
tokenManager.clearTokens()
|
||||
setUser(null)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch user info:', error)
|
||||
// If there's an error, clear tokens to be safe
|
||||
tokenManager.clearTokens()
|
||||
setUser(null)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,11 +148,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const data = await response.json()
|
||||
|
||||
// Store tokens in TokenManager
|
||||
tokenManager.setTokens(
|
||||
data.access_token,
|
||||
data.refresh_token,
|
||||
data.expires_in
|
||||
)
|
||||
tokenManager.setTokens(data.access_token, data.refresh_token)
|
||||
|
||||
// Fetch user info
|
||||
await fetchUserInfo()
|
||||
@@ -161,8 +165,9 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
tokenManager.logout()
|
||||
// Token manager will emit 'logout' event which we handle above
|
||||
tokenManager.clearTokens()
|
||||
setUser(null)
|
||||
router.push('/login')
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -69,8 +69,15 @@ export function ModulesProvider({ children }: { children: ReactNode }) {
|
||||
setLastUpdated(new Date())
|
||||
|
||||
} catch (err) {
|
||||
// If we get a 401 error, clear the tokens
|
||||
if (err && typeof err === 'object' && 'response' in err && (err.response as any)?.status === 401) {
|
||||
tokenManager.clearTokens()
|
||||
setModules([])
|
||||
setEnabledModules(new Set())
|
||||
setError(null)
|
||||
setLastUpdated(null)
|
||||
} else if (tokenManager.isAuthenticated()) {
|
||||
// Only set error if we're authenticated (to avoid noise on auth pages)
|
||||
if (tokenManager.isAuthenticated()) {
|
||||
setError(err instanceof Error ? err.message : "Failed to load modules")
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user