39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { create } from "zustand";
|
|
import { persist } from "zustand/middleware";
|
|
|
|
export interface Customer {
|
|
id: number;
|
|
lineUserId: string;
|
|
displayName: string | null;
|
|
pictureUrl: string | null;
|
|
email: string | null;
|
|
phone: string | null;
|
|
points?: number;
|
|
}
|
|
|
|
interface AuthState {
|
|
token: string | null;
|
|
customer: Customer | null;
|
|
isLiffInitialized: boolean;
|
|
setAuth: (token: string, customer: Customer) => void;
|
|
setLiffInitialized: (status: boolean) => void;
|
|
logout: () => void;
|
|
}
|
|
|
|
export const useAuthStore = create<AuthState>()(
|
|
persist(
|
|
(set) => ({
|
|
token: null,
|
|
customer: null,
|
|
isLiffInitialized: false,
|
|
setAuth: (token, customer) => set({ token, customer }),
|
|
setLiffInitialized: (isLiffInitialized) => set({ isLiffInitialized }),
|
|
logout: () => set({ token: null, customer: null }),
|
|
}),
|
|
{
|
|
name: "app-auth-storage",
|
|
partialize: (state) => ({ token: state.token, customer: state.customer }), // don't persist isLiffInitialized
|
|
}
|
|
)
|
|
);
|