- Express + TypeScript backend with IRC connection manager - React + Vite frontend with mIRC-inspired theme - Drizzle ORM + PostgreSQL schema (users, connections, channels, messages) - Redis pub/sub for real-time WebSocket delivery across replicas - IRC color code parser (16 + 99 extended palette) - Kubernetes manifests for homecloud deployment - Multi-stage Dockerfile with non-root user - docker-compose for local development
95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { create } from 'zustand';
|
|
|
|
interface User {
|
|
id: string;
|
|
username: string;
|
|
email: string;
|
|
}
|
|
|
|
interface AuthState {
|
|
token: string | null;
|
|
user: User | null;
|
|
isLoading: boolean;
|
|
error: string | null;
|
|
login: (email: string, password: string) => Promise<void>;
|
|
register: (username: string, email: string, password: string) => Promise<void>;
|
|
logout: () => void;
|
|
loadFromStorage: () => void;
|
|
clearError: () => void;
|
|
}
|
|
|
|
export const useAuthStore = create<AuthState>((set, get) => ({
|
|
token: null,
|
|
user: null,
|
|
isLoading: false,
|
|
error: null,
|
|
|
|
login: async (email: string, password: string) => {
|
|
set({ isLoading: true, error: null });
|
|
try {
|
|
const res = await fetch('/api/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(() => ({ message: 'Login failed' }));
|
|
throw new Error(data.message || `Login failed (${res.status})`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
localStorage.setItem('token', data.token);
|
|
localStorage.setItem('user', JSON.stringify(data.user));
|
|
set({ token: data.token, user: data.user, isLoading: false });
|
|
} catch (err: any) {
|
|
set({ error: err.message, isLoading: false });
|
|
}
|
|
},
|
|
|
|
register: async (username: string, email: string, password: string) => {
|
|
set({ isLoading: true, error: null });
|
|
try {
|
|
const res = await fetch('/api/auth/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, email, password }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const data = await res.json().catch(() => ({ message: 'Registration failed' }));
|
|
throw new Error(data.message || `Registration failed (${res.status})`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
localStorage.setItem('token', data.token);
|
|
localStorage.setItem('user', JSON.stringify(data.user));
|
|
set({ token: data.token, user: data.user, isLoading: false });
|
|
} catch (err: any) {
|
|
set({ error: err.message, isLoading: false });
|
|
}
|
|
},
|
|
|
|
logout: () => {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
set({ token: null, user: null });
|
|
},
|
|
|
|
loadFromStorage: () => {
|
|
const token = localStorage.getItem('token');
|
|
const userStr = localStorage.getItem('user');
|
|
if (token && userStr) {
|
|
try {
|
|
const user = JSON.parse(userStr);
|
|
set({ token, user });
|
|
} catch {
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
}
|
|
}
|
|
},
|
|
|
|
clearError: () => set({ error: null }),
|
|
}));
|