| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import { create } from 'zustand'
- import { getCookie, setCookie, removeCookie } from '@/lib/cookies'
- const ACCESS_TOKEN = 'thisisjustarandomstring'
- interface AuthUser {
- accountNo: string
- email: string
- role: string[]
- exp: number
- }
- interface AuthState {
- auth: {
- user: AuthUser | null
- setUser: (user: AuthUser | null) => void
- accessToken: string
- setAccessToken: (accessToken: string) => void
- resetAccessToken: () => void
- reset: () => void
- }
- }
- export const useAuthStore = create<AuthState>()((set) => {
- const cookieState = getCookie(ACCESS_TOKEN)
- const initToken = cookieState ? JSON.parse(cookieState) : ''
- return {
- auth: {
- user: null,
- setUser: (user) =>
- set((state) => ({ ...state, auth: { ...state.auth, user } })),
- accessToken: initToken,
- setAccessToken: (accessToken) =>
- set((state) => {
- setCookie(ACCESS_TOKEN, JSON.stringify(accessToken))
- return { ...state, auth: { ...state.auth, accessToken } }
- }),
- resetAccessToken: () =>
- set((state) => {
- removeCookie(ACCESS_TOKEN)
- return { ...state, auth: { ...state.auth, accessToken: '' } }
- }),
- reset: () =>
- set((state) => {
- removeCookie(ACCESS_TOKEN)
- return {
- ...state,
- auth: { ...state.auth, user: null, accessToken: '' },
- }
- }),
- },
- }
- })
|