37 lines
979 B
TypeScript
37 lines
979 B
TypeScript
'use client';
|
|
|
|
import { create } from 'zustand';
|
|
import { persist, createJSONStorage } from 'zustand/middleware';
|
|
import { User } from '../types';
|
|
import { MOCK_USERS } from './mock-data';
|
|
|
|
interface AuthState {
|
|
user: User | null;
|
|
login: (username: string, password: string) => boolean;
|
|
logout: () => void;
|
|
}
|
|
|
|
export const useAuthStore = create<AuthState>()(
|
|
persist(
|
|
(set) => ({
|
|
user: null,
|
|
login: (username, password) => {
|
|
if (!username || !password) return false;
|
|
if (password !== username) return false;
|
|
const foundUser = MOCK_USERS.find(
|
|
(u) => u.username.toLowerCase() === username.toLowerCase()
|
|
);
|
|
if (!foundUser) return false;
|
|
set({ user: foundUser });
|
|
return true;
|
|
},
|
|
logout: () => set({ user: null }),
|
|
}),
|
|
{
|
|
name: 'patchpilot:auth',
|
|
storage: createJSONStorage(() => localStorage),
|
|
partialize: (state) => ({ user: state.user }),
|
|
}
|
|
)
|
|
);
|