import { create } from 'zustand'; interface AuthState { token: string | null; username: string | null; deviceId: string | null; isAuthenticated: boolean; login: (token: string, username: string, deviceId?: string | null) => void; setDeviceId: (deviceId: string | null) => void; logout: () => void; } export const useAuthStore = create((set) => { const storedToken = localStorage.getItem('auth_token'); const storedUsername = localStorage.getItem('auth_username'); const storedDeviceId = localStorage.getItem('auth_device_id'); return { token: storedToken, username: storedUsername, deviceId: storedDeviceId, isAuthenticated: !!storedToken, login: (token, username, deviceId = null) => { localStorage.setItem('auth_token', token); localStorage.setItem('auth_username', username); if (deviceId) { localStorage.setItem('auth_device_id', deviceId); } set({ token, username, deviceId: deviceId || storedDeviceId, isAuthenticated: true }); }, setDeviceId: (deviceId) => { if (deviceId) { localStorage.setItem('auth_device_id', deviceId); } else { localStorage.removeItem('auth_device_id'); } set({ deviceId }); }, logout: () => { localStorage.removeItem('auth_token'); localStorage.removeItem('auth_username'); // Note: we don't remove auth_device_id here because it's used for auto-login set({ token: null, username: null, isAuthenticated: false }); }, }; });