Scaffold @forte/admin package with React, Vite, shadcn/ui, TanStack Router
Sets up the admin frontend with login page, auth guard, API client, Zustand auth store, and all shadcn/ui components. Vite proxies /v1 to backend in dev.
This commit is contained in:
37
packages/admin/src/stores/auth.store.ts
Normal file
37
packages/admin/src/stores/auth.store.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { create } from 'zustand'
|
||||
|
||||
interface User {
|
||||
id: string
|
||||
email: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
role: string
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
token: string | null
|
||||
user: User | null
|
||||
companyId: string | null
|
||||
setAuth: (token: string, user: User) => void
|
||||
logout: () => void
|
||||
}
|
||||
|
||||
function decodeJwtPayload(token: string): { id: string; companyId: string; role: string } {
|
||||
const payload = token.split('.')[1]
|
||||
return JSON.parse(atob(payload))
|
||||
}
|
||||
|
||||
export const useAuthStore = create<AuthState>((set) => ({
|
||||
token: null,
|
||||
user: null,
|
||||
companyId: null,
|
||||
|
||||
setAuth: (token, user) => {
|
||||
const payload = decodeJwtPayload(token)
|
||||
set({ token, user, companyId: payload.companyId })
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
set({ token: null, user: null, companyId: null })
|
||||
},
|
||||
}))
|
||||
Reference in New Issue
Block a user