Refactor project structure and dependencies; rename package to azaion-ui, update version to 0.0.1, and remove unused files. Introduce new routing and authentication features in App component.

This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-03-25 03:10:15 +02:00
parent e407308284
commit 157a33096a
112 changed files with 6530 additions and 17843 deletions
+54
View File
@@ -0,0 +1,54 @@
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from 'react'
import { api, setToken } from '../api/client'
import type { AuthUser } from '../types'
interface AuthState {
user: AuthUser | null
loading: boolean
login: (email: string, password: string) => Promise<void>
logout: () => Promise<void>
hasPermission: (perm: string) => boolean
}
const AuthContext = createContext<AuthState>(null!)
export function useAuth() {
return useContext(AuthContext)
}
export function AuthProvider({ children }: { children: ReactNode }) {
const [user, setUser] = useState<AuthUser | null>(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
api.get<{ user: AuthUser; token: string }>('/api/admin/auth/refresh')
.then(data => {
setToken(data.token)
setUser(data.user)
})
.catch(() => {})
.finally(() => setLoading(false))
}, [])
const login = useCallback(async (email: string, password: string) => {
const data = await api.post<{ token: string; user: AuthUser }>('/api/admin/auth/login', { email, password })
setToken(data.token)
setUser(data.user)
}, [])
const logout = useCallback(async () => {
try { await api.post('/api/admin/auth/logout') } catch {}
setToken(null)
setUser(null)
}, [])
const hasPermission = useCallback((perm: string) => {
return user?.permissions.includes(perm) ?? false
}, [user])
return (
<AuthContext.Provider value={{ user, loading, login, logout, hasPermission }}>
{children}
</AuthContext.Provider>
)
}