File size: 1,115 Bytes
fc1eb7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import { apiRequest } from "./queryClient";
import { User } from "@shared/schema";
export interface AuthResponse {
user: User;
token: string;
}
export async function login(email: string, password: string): Promise<AuthResponse> {
const response = await apiRequest("POST", "/api/auth/login", { email, password });
const data = await response.json();
// Store token in localStorage
localStorage.setItem("token", data.token);
return data;
}
export async function register(name: string, email: string, password: string): Promise<AuthResponse> {
const response = await apiRequest("POST", "/api/auth/register", { name, email, password, role: "user" });
const data = await response.json();
// Store token in localStorage
localStorage.setItem("token", data.token);
return data;
}
export async function logout(): Promise<void> {
// Remove token from localStorage
localStorage.removeItem("token");
}
export function getToken(): string | null {
return localStorage.getItem("token");
}
export function isAuthenticated(): boolean {
const token = getToken();
return !!token;
}
|