generated from muhtadeetaron/nextjs-template
96 lines
2.4 KiB
TypeScript
96 lines
2.4 KiB
TypeScript
import { LoginForm, RegisterForm } from "@/types/auth";
|
|
|
|
export const API_URL = "https://examjam-backend.omukk.dev";
|
|
|
|
// Cookie utility function
|
|
const setCookie = (name: string, value: string | null, days: number = 7) => {
|
|
if (typeof document === "undefined") return;
|
|
|
|
if (value === null) {
|
|
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; SameSite=Strict; Secure`;
|
|
} else {
|
|
const expires = new Date();
|
|
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
|
|
document.cookie = `${name}=${encodeURIComponent(
|
|
value
|
|
)}; expires=${expires.toUTCString()}; path=/; SameSite=Strict; Secure`;
|
|
}
|
|
};
|
|
|
|
type SetTokenFn = (token: string) => void;
|
|
|
|
// Optional: Create a custom error type to carry extra data
|
|
interface APIError extends Error {
|
|
response?: any;
|
|
}
|
|
|
|
export const login = async (
|
|
form: LoginForm,
|
|
setToken: SetTokenFn
|
|
): Promise<void> => {
|
|
const response = await fetch(`${API_URL}/auth/login`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(form),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.message || "Login failed");
|
|
}
|
|
|
|
setCookie("authToken", data.token);
|
|
setToken(data.token);
|
|
};
|
|
|
|
export const register = async (
|
|
form: RegisterForm,
|
|
setToken: SetTokenFn
|
|
): Promise<void> => {
|
|
const response = await fetch(`${API_URL}/auth/register`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(form),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
const error: APIError = new Error(data?.detail || "Registration failed");
|
|
error.response = data;
|
|
throw error;
|
|
}
|
|
|
|
setCookie("authToken", data.token);
|
|
setToken(data.token);
|
|
};
|
|
|
|
export const getTokenFromCookie = (): string | null => {
|
|
if (typeof document === "undefined") return null;
|
|
|
|
const value = `; ${document.cookie}`;
|
|
const parts = value.split(`; authToken=`);
|
|
if (parts.length === 2) {
|
|
return parts.pop()?.split(";").shift() || null;
|
|
}
|
|
return null;
|
|
};
|
|
|
|
export const clearAuthToken = (): void => {
|
|
setCookie("authToken", null);
|
|
};
|
|
|
|
export const getToken = async (): Promise<string | null> => {
|
|
if (typeof window === "undefined") {
|
|
return null;
|
|
}
|
|
|
|
const match = document.cookie.match(/(?:^|;\s*)authToken=([^;]*)/);
|
|
return match ? decodeURIComponent(match[1]) : null;
|
|
};
|