Frontend: React+Vite+Tailwind, Login, Dashboard, Agents, Customers, Settings, Dark Theme, Nginx auf .20
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { create } from 'zustand'
|
||||
import api from '../api/client'
|
||||
|
||||
export const useAuthStore = create((set) => ({
|
||||
user: null,
|
||||
isAuthenticated: !!api.getToken(),
|
||||
loading: true,
|
||||
|
||||
login: async (username, password) => {
|
||||
const data = await api.login(username, password)
|
||||
set({ user: data.user, isAuthenticated: true })
|
||||
return data
|
||||
},
|
||||
|
||||
logout: () => {
|
||||
api.logout()
|
||||
set({ user: null, isAuthenticated: false })
|
||||
},
|
||||
|
||||
checkAuth: async () => {
|
||||
if (!api.getToken()) {
|
||||
set({ loading: false, isAuthenticated: false })
|
||||
return
|
||||
}
|
||||
try {
|
||||
const data = await api.me()
|
||||
set({ user: data.user || data, isAuthenticated: true, loading: false })
|
||||
} catch {
|
||||
api.logout()
|
||||
set({ user: null, isAuthenticated: false, loading: false })
|
||||
}
|
||||
},
|
||||
}))
|
||||
Reference in New Issue
Block a user