popo
This commit is contained in:
parent
30e29363a4
commit
ae3b96d3bb
1 changed files with 32 additions and 0 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from "react"
|
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from "react"
|
||||||
import { useSession, signIn, signOut } from "next-auth/react"
|
import { useSession, signIn, signOut } from "next-auth/react"
|
||||||
|
import { usePathname } from "next/navigation"
|
||||||
import type { User, Ticket, TicketStatus, Room } from "./types"
|
import type { User, Ticket, TicketStatus, Room } from "./types"
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api"
|
const API_URL = process.env.NEXT_PUBLIC_API_URL + "/api"
|
||||||
|
|
@ -23,6 +24,7 @@ interface AuthContextType {
|
||||||
updateUserRole: (userId: number, role: string) => Promise<boolean>
|
updateUserRole: (userId: number, role: string) => Promise<boolean>
|
||||||
deleteUser: (userId: number) => Promise<boolean>
|
deleteUser: (userId: number) => Promise<boolean>
|
||||||
adminResetPassword: (userId: number, password: string) => Promise<boolean>
|
adminResetPassword: (userId: number, password: string) => Promise<boolean>
|
||||||
|
validateSession: () => Promise<boolean>
|
||||||
}
|
}
|
||||||
|
|
||||||
const AuthContext = createContext<AuthContextType | null>(null)
|
const AuthContext = createContext<AuthContextType | null>(null)
|
||||||
|
|
@ -32,6 +34,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
const [tickets, setTickets] = useState<Ticket[]>([])
|
const [tickets, setTickets] = useState<Ticket[]>([])
|
||||||
const [rooms, setRooms] = useState<Room[]>([])
|
const [rooms, setRooms] = useState<Room[]>([])
|
||||||
const [authHeader, setAuthHeader] = useState<string | null>(null)
|
const [authHeader, setAuthHeader] = useState<string | null>(null)
|
||||||
|
const pathname = usePathname()
|
||||||
|
|
||||||
// Fetch rooms on mount
|
// Fetch rooms on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -284,6 +287,34 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
}, [authHeader])
|
}, [authHeader])
|
||||||
|
|
||||||
|
const validateSession = useCallback(async () => {
|
||||||
|
if (!authHeader) return false
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_URL}/auth/me`, {
|
||||||
|
headers: { "Authorization": authHeader }
|
||||||
|
})
|
||||||
|
if (res.ok) {
|
||||||
|
const freshUser = await res.json()
|
||||||
|
setUser(freshUser)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
// If the checking fails (401/403), log the user out
|
||||||
|
logout()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Session validation failed", e)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}, [authHeader, logout])
|
||||||
|
|
||||||
|
// Validate session on route change
|
||||||
|
useEffect(() => {
|
||||||
|
if (authHeader) {
|
||||||
|
validateSession()
|
||||||
|
}
|
||||||
|
}, [pathname, authHeader, validateSession])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider
|
<AuthContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
|
@ -303,6 +334,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
updateUserRole,
|
updateUserRole,
|
||||||
deleteUser,
|
deleteUser,
|
||||||
adminResetPassword,
|
adminResetPassword,
|
||||||
|
validateSession,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue