|
@@ -1,6 +1,6 @@
|
|
|
import { create } from 'zustand'
|
|
import { create } from 'zustand'
|
|
|
import { persist } from 'zustand/middleware'
|
|
import { persist } from 'zustand/middleware'
|
|
|
-import type { AdminUser, LoginResponse } from '@/types/auth'
|
|
|
|
|
|
|
+import type { AdminUser, LoginResponse } from '@/types/auth.types'
|
|
|
import { http } from '@/core/api/http'
|
|
import { http } from '@/core/api/http'
|
|
|
|
|
|
|
|
interface LoginPayload {
|
|
interface LoginPayload {
|
|
@@ -14,6 +14,9 @@ interface AuthState {
|
|
|
isAuthenticated: boolean
|
|
isAuthenticated: boolean
|
|
|
loading: boolean
|
|
loading: boolean
|
|
|
error: string | null
|
|
error: string | null
|
|
|
|
|
+ isHydrated: boolean
|
|
|
|
|
+ profileFetched: boolean
|
|
|
|
|
+
|
|
|
login: (data: LoginPayload) => Promise<void>
|
|
login: (data: LoginPayload) => Promise<void>
|
|
|
logout: () => void
|
|
logout: () => void
|
|
|
fetchProfile: () => Promise<void>
|
|
fetchProfile: () => Promise<void>
|
|
@@ -21,12 +24,14 @@ interface AuthState {
|
|
|
|
|
|
|
|
export const useAuthStore = create<AuthState>()(
|
|
export const useAuthStore = create<AuthState>()(
|
|
|
persist(
|
|
persist(
|
|
|
- (set) => ({
|
|
|
|
|
|
|
+ (set, get) => ({
|
|
|
user: null,
|
|
user: null,
|
|
|
token: null,
|
|
token: null,
|
|
|
isAuthenticated: false,
|
|
isAuthenticated: false,
|
|
|
loading: false,
|
|
loading: false,
|
|
|
error: null,
|
|
error: null,
|
|
|
|
|
+ isHydrated: false,
|
|
|
|
|
+ profileFetched: false,
|
|
|
|
|
|
|
|
login: async (data: LoginPayload) => {
|
|
login: async (data: LoginPayload) => {
|
|
|
set({ loading: true, error: null })
|
|
set({ loading: true, error: null })
|
|
@@ -45,6 +50,7 @@ export const useAuthStore = create<AuthState>()(
|
|
|
isAuthenticated: true,
|
|
isAuthenticated: true,
|
|
|
loading: false,
|
|
loading: false,
|
|
|
error: null,
|
|
error: null,
|
|
|
|
|
+ profileFetched: true,
|
|
|
})
|
|
})
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
} catch (error: any) {
|
|
} catch (error: any) {
|
|
@@ -66,20 +72,35 @@ export const useAuthStore = create<AuthState>()(
|
|
|
token: null,
|
|
token: null,
|
|
|
isAuthenticated: false,
|
|
isAuthenticated: false,
|
|
|
error: null,
|
|
error: null,
|
|
|
|
|
+ profileFetched: false,
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
fetchProfile: async () => {
|
|
fetchProfile: async () => {
|
|
|
|
|
+ const { profileFetched } = get()
|
|
|
|
|
+
|
|
|
|
|
+ if (profileFetched) return
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
const res = await http.get<{ data: AdminUser }>('/admin/auth/profile')
|
|
const res = await http.get<{ data: AdminUser }>('/admin/auth/profile')
|
|
|
|
|
|
|
|
set({
|
|
set({
|
|
|
user: res.data,
|
|
user: res.data,
|
|
|
isAuthenticated: true,
|
|
isAuthenticated: true,
|
|
|
|
|
+ profileFetched: true,
|
|
|
})
|
|
})
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
// eslint-disable-next-line no-console
|
|
// eslint-disable-next-line no-console
|
|
|
console.log('Profile fetch failed', error)
|
|
console.log('Profile fetch failed', error)
|
|
|
|
|
+
|
|
|
|
|
+ set({
|
|
|
|
|
+ user: null,
|
|
|
|
|
+ token: null,
|
|
|
|
|
+ isAuthenticated: false,
|
|
|
|
|
+ profileFetched: false,
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ throw error
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
}),
|
|
}),
|
|
@@ -91,6 +112,12 @@ export const useAuthStore = create<AuthState>()(
|
|
|
token: state.token,
|
|
token: state.token,
|
|
|
isAuthenticated: state.isAuthenticated,
|
|
isAuthenticated: state.isAuthenticated,
|
|
|
}),
|
|
}),
|
|
|
|
|
+
|
|
|
|
|
+ onRehydrateStorage: () => (state) => {
|
|
|
|
|
+ if (state) {
|
|
|
|
|
+ state.isHydrated = true
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
)
|
|
)
|