Files
todo-app-web/src/stores/auth.ts
Hammer 5a4d7e0ba9
Some checks failed
CI/CD / test (push) Failing after 1m23s
CI/CD / deploy (push) Has been skipped
fix: resolve ESLint errors for CI
- Remove unused imports (Flag, Tag, Hash, User, FolderPlus, Check, Plus, Link, cn, formatDate, getPriorityLabel)
- Remove unused variable (inbox in Sidebar)
- Fix empty catch block with comment
- Replace any types with proper Mock/Record types in tests
- Suppress set-state-in-effect for intentional form state sync
- Remove unused get parameter from zustand store
2026-01-30 03:00:17 +00:00

91 lines
2.1 KiB
TypeScript

import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import type { User } from '@/types';
import { api } from '@/lib/api';
interface AuthState {
user: User | null;
isLoading: boolean;
isAuthenticated: boolean;
// Actions
setUser: (user: User | null) => void;
login: (email: string, password: string) => Promise<void>;
logout: () => Promise<void>;
checkSession: () => Promise<void>;
}
export const useAuthStore = create<AuthState>()(
persist(
(set) => ({
user: null,
isLoading: true,
isAuthenticated: false,
setUser: (user) => set({
user,
isAuthenticated: !!user,
isLoading: false,
}),
login: async (email, password) => {
set({ isLoading: true });
try {
await api.login(email, password);
const session = await api.getSession();
if (session?.user) {
set({
user: session.user,
isAuthenticated: true,
isLoading: false,
});
} else {
throw new Error('Failed to get session');
}
} catch (error) {
set({ isLoading: false });
throw error;
}
},
logout: async () => {
try {
await api.logout();
} finally {
set({ user: null, isAuthenticated: false });
}
},
checkSession: async () => {
set({ isLoading: true });
try {
const session = await api.getSession();
if (session?.user) {
set({
user: session.user,
isAuthenticated: true,
isLoading: false,
});
} else {
set({
user: null,
isAuthenticated: false,
isLoading: false,
});
}
} catch {
set({
user: null,
isAuthenticated: false,
isLoading: false,
});
}
},
}),
{
name: 'auth-storage',
partialize: (state) => ({ user: state.user }),
}
)
);