Initial commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useState } from 'react'
|
||||
import * as api from '@/services/api'
|
||||
import { isUnauthorized } from '@/services/errors'
|
||||
import type { LoginRequest, RegisterRequest, TokenResponse } from '@/types'
|
||||
|
||||
interface AuthState {
|
||||
accessToken: string | null
|
||||
isAuthenticated: boolean
|
||||
isLoading: boolean
|
||||
}
|
||||
|
||||
interface AuthContextValue extends AuthState {
|
||||
login: (req: LoginRequest) => Promise<void>
|
||||
register: (req: RegisterRequest) => Promise<void>
|
||||
logout: () => Promise<void>
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null)
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [state, setState] = useState<AuthState>({
|
||||
accessToken: null,
|
||||
isAuthenticated: false,
|
||||
isLoading: true,
|
||||
})
|
||||
|
||||
// On mount, try to refresh the access token from the httpOnly refresh cookie.
|
||||
useEffect(() => {
|
||||
api
|
||||
.refreshToken()
|
||||
.then((res: TokenResponse) => {
|
||||
api.setAccessToken(res.accessToken)
|
||||
setState({ accessToken: res.accessToken, isAuthenticated: true, isLoading: false })
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (isUnauthorized(err)) {
|
||||
// No valid refresh token — user must log in.
|
||||
setState({ accessToken: null, isAuthenticated: false, isLoading: false })
|
||||
} else {
|
||||
setState({ accessToken: null, isAuthenticated: false, isLoading: false })
|
||||
}
|
||||
})
|
||||
}, [])
|
||||
|
||||
const handleTokenResponse = useCallback((res: TokenResponse) => {
|
||||
api.setAccessToken(res.accessToken)
|
||||
setState({ accessToken: res.accessToken, isAuthenticated: true, isLoading: false })
|
||||
}, [])
|
||||
|
||||
const login = useCallback(
|
||||
async (req: LoginRequest) => {
|
||||
const res = await api.login(req)
|
||||
handleTokenResponse(res)
|
||||
},
|
||||
[handleTokenResponse],
|
||||
)
|
||||
|
||||
const register = useCallback(
|
||||
async (req: RegisterRequest) => {
|
||||
const res = await api.register(req)
|
||||
handleTokenResponse(res)
|
||||
},
|
||||
[handleTokenResponse],
|
||||
)
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
await api.logout()
|
||||
api.setAccessToken(null)
|
||||
setState({ accessToken: null, isAuthenticated: false, isLoading: false })
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ ...state, login, register, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const ctx = useContext(AuthContext)
|
||||
if (!ctx) throw new Error('useAuth must be used inside <AuthProvider>')
|
||||
return ctx
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { createContext, useCallback, useContext, useEffect, useState } from 'react'
|
||||
import * as api from '@/services/api'
|
||||
import { useAuth } from '@/context/AuthContext'
|
||||
import type { Workspace } from '@/types'
|
||||
|
||||
interface WorkspaceContextValue {
|
||||
workspace: Workspace | null
|
||||
isLoading: boolean
|
||||
error: string | null
|
||||
setWorkspaceId: (id: string) => void
|
||||
refetch: () => void
|
||||
}
|
||||
|
||||
const WorkspaceContext = createContext<WorkspaceContextValue | null>(null)
|
||||
|
||||
export function WorkspaceProvider({ children }: { children: React.ReactNode }) {
|
||||
const { isAuthenticated } = useAuth()
|
||||
const [workspaceId, setWorkspaceId] = useState<string | null>(
|
||||
() => localStorage.getItem('workspaceId'),
|
||||
)
|
||||
const [workspace, setWorkspace] = useState<Workspace | null>(null)
|
||||
const [isLoading, setIsLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const fetchWorkspace = useCallback(async (id: string) => {
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
try {
|
||||
const ws = await api.getWorkspace(id)
|
||||
setWorkspace(ws)
|
||||
} catch (err: unknown) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to load workspace')
|
||||
setWorkspace(null)
|
||||
} finally {
|
||||
setIsLoading(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (isAuthenticated && workspaceId) {
|
||||
void fetchWorkspace(workspaceId)
|
||||
}
|
||||
}, [isAuthenticated, workspaceId, fetchWorkspace])
|
||||
|
||||
const handleSetWorkspaceId = useCallback((id: string) => {
|
||||
localStorage.setItem('workspaceId', id)
|
||||
setWorkspaceId(id)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<WorkspaceContext.Provider
|
||||
value={{
|
||||
workspace,
|
||||
isLoading,
|
||||
error,
|
||||
setWorkspaceId: handleSetWorkspaceId,
|
||||
refetch: () => { if (workspaceId) void fetchWorkspace(workspaceId) },
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</WorkspaceContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useWorkspace(): WorkspaceContextValue {
|
||||
const ctx = useContext(WorkspaceContext)
|
||||
if (!ctx) throw new Error('useWorkspace must be used inside <WorkspaceProvider>')
|
||||
return ctx
|
||||
}
|
||||
Reference in New Issue
Block a user