gohorsejobs/frontend/src/lib/auth.ts

314 lines
10 KiB
TypeScript

import { User } from "./types";
import { getApiUrl } from "./config";
export type { User };
const AUTH_KEY = "job-portal-auth";
// API URL now uses runtime config
const getApiV1Url = () => `${getApiUrl()}/api/v1`;
interface LoginResponse {
token: string;
user: {
id: string;
name: string;
email: string;
roles: string[];
status: string;
created_at: string;
avatar_url?: string;
};
}
export async function login(
email: string,
password: string,
role?: "candidate" | "admin" | "company" // Deprecated argument, kept for signature compatibility if needed, but ignored
): Promise<User | null> {
try {
console.log("%c[AUTH] Attempting login...", "color: #3b82f6; font-weight: bold", { email });
const res = await fetch(`${getApiV1Url()}/auth/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
credentials: "include", // Send and receive cookies
body: JSON.stringify({ email, password }),
});
if (!res.ok) {
if (res.status === 401) {
throw new Error("AUTH_INVALID_CREDENTIALS");
}
throw new Error("AUTH_SERVER_ERROR");
}
const data: LoginResponse = await res.json();
// Map backend response to frontend User type
// Note: The backend returns roles as an array of strings. The frontend expects a single 'role' or we need to adapt.
// For now we map the first role or main role to the 'role' field.
let userRole: "candidate" | "admin" | "company" | "superadmin" = "candidate";
// Check for SuperAdmin (Platform Admin)
if (data.user.roles.includes("superadmin") || data.user.roles.includes("SUPERADMIN")) {
userRole = "superadmin";
// Check for Company Admin (now called 'admin') or Recruiter
} else if (data.user.roles.includes("admin") || data.user.roles.includes("recruiter")) {
userRole = "company";
}
const user: User = {
id: data.user.id,
name: data.user.name,
email: data.user.email,
role: userRole,
roles: data.user.roles, // Extend User type if needed, or just keep it here
avatarUrl: data.user.avatar_url,
profileComplete: 80, // Mocked for now
};
// Store user info in sessionStorage (not token - token is in httpOnly cookie)
if (typeof window !== "undefined") {
localStorage.setItem(AUTH_KEY, JSON.stringify(user));
}
return user;
} catch (error) {
console.error("%c[AUTH] Login Error:", "color: #ef4444; font-weight: bold", error);
throw error;
}
}
export async function logout(): Promise<void> {
if (typeof window !== "undefined") {
localStorage.removeItem(AUTH_KEY);
// Call backend to clear the httpOnly cookie
try {
await fetch(`${getApiV1Url()}/auth/logout`, {
method: "POST",
credentials: "include",
});
} catch (error) {
console.error("[AUTH] Logout error:", error);
}
}
}
export function getCurrentUser(): User | null {
if (typeof window !== "undefined") {
const stored = localStorage.getItem(AUTH_KEY);
if (stored) {
const user = JSON.parse(stored);
// console.log("%c[AUTH] User Loaded from Storage", "color: #10b981", user.email);
return user;
}
// User not in storage (normal state)
console.log("%c[AUTH] No user session found", "color: #94a3b8");
}
return null;
}
// Helper function to map backend roles to frontend role
function mapRoleFromBackend(roles: string[]): "candidate" | "admin" | "company" | "superadmin" {
if (roles.includes("superadmin") || roles.includes("SUPERADMIN")) {
return "superadmin";
}
if (roles.includes("admin") || roles.includes("recruiter")) {
return "company";
}
return "candidate";
}
/**
* Refreshes the session by calling /users/me with the HTTPOnly cookie.
* Use this on app mount to restore session across tabs/reloads.
*/
export async function refreshSession(): Promise<User | null> {
try {
// Ensure runtime config is loaded before making the request
if (typeof window !== "undefined") {
await import("./config").then((m) => m.initConfig());
}
console.log("%c[AUTH] Attempting to refresh session...", "color: #3b82f6");
const res = await fetch(`${getApiV1Url()}/users/me`, {
method: "GET",
credentials: "include", // Send HTTPOnly cookie
});
if (!res.ok) {
// Cookie expired or invalid - clear local storage
console.log("%c[AUTH] Session refresh: No session", "color: #94a3b8", res.status);
localStorage.removeItem(AUTH_KEY);
return null;
}
const userData = await res.json();
const user: User = {
id: userData.id,
name: userData.name || userData.full_name,
email: userData.email,
role: mapRoleFromBackend(userData.roles || []),
roles: userData.roles || [],
avatarUrl: userData.avatarUrl || userData.avatar_url,
profileComplete: 80,
};
localStorage.setItem(AUTH_KEY, JSON.stringify(user));
console.log("%c[AUTH] Session restored from cookie", "color: #10b981", user.email);
return user;
} catch (error) {
// Only log for non-network errors in development
// "Failed to fetch" is expected when API is unreachable (normal for unauthenticated users)
if (error instanceof TypeError && error.message.includes('Failed to fetch')) {
// Silent fail for network errors - this is expected
console.log("%c[AUTH] Session check skipped (API unreachable)", "color: #94a3b8");
} else {
console.error("%c[AUTH] Failed to refresh session:", "color: #ef4444", error);
}
return null;
}
}
export function isAdminUser(user: User | null): boolean {
if (!user) return false;
const roles = user.roles || [];
return (
user.role === "admin" ||
roles.includes("superadmin") ||
roles.includes("admin") ||
roles.includes("ADMIN") ||
roles.includes("SUPERADMIN")
);
}
export function isAuthenticated(): boolean {
return getCurrentUser() !== null;
}
export interface RegisterCandidateData {
name: string;
email: string;
password: string;
username: string; // identifier
phone: string;
birthDate?: string;
address?: string;
city?: string;
state?: string;
zipCode?: string;
education?: string;
experience?: string;
skills?: string;
objective?: string;
}
export async function registerCandidate(data: RegisterCandidateData): Promise<void> {
console.log('[registerCandidate] Sending request:', { ...data, password: '***' });
const res = await fetch(`${getApiV1Url()}/auth/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
console.error('[registerCandidate] Error response:', res.status, errorData);
throw new Error(errorData.message || `Registration failed: ${res.status}`);
}
const responseData = await res.json().catch(() => ({}));
console.log('[registerCandidate] Success response:', {
...responseData,
token: responseData.token ? '***' : undefined
});
}
export function getToken(): string | null {
// Token is now in httpOnly cookie, not accessible from JS
// This function is kept for backward compatibility but returns null
return null;
}
// Company Registration
export interface RegisterCompanyData {
companyName: string;
email: string;
phone: string;
password?: string;
confirmPassword?: string;
document?: string; // cnpj
website?: string;
yearsInMarket?: string;
description?: string;
zipCode?: string;
address?: string;
city?: string;
state?: string;
birthDate?: string;
cnpj?: string; // alias for document
}
export async function registerCompany(data: RegisterCompanyData): Promise<void> {
console.log('[registerCompany] Sending request:', { ...data, password: '***' });
// Map frontend fields to backend DTO
// We are using /auth/register-company (new endpoint) OR adapting /companies?
// Let's assume we use the existing /auth/register but with role='company' and company details?
// Or if the backend supports /companies creating a user.
// Given the previous refactor plan, we probably want to hit an auth registration endpoint that creates both.
// Let's check if there is a specific handler for this.
// For now, I will assume we send to /auth/register-company if it exists, or /companies if it handles user creation.
// Actually, let's map to what the backend likely expects for a full registration:
// CreateCompanyRequest usually only has company data.
// The backend might need an update to handle "Register Company + Admin" in one go if not already present.
// Let's stick to the payload structure and verify backend later.
const payload = {
name: data.companyName,
slug: data.companyName.toLowerCase().replace(/\s+/g, '-'), // Generate slug
document: data.document || data.cnpj,
phone: data.phone,
email: data.email, // Company email
website: data.website,
address: data.address,
zip_code: data.zipCode,
city: data.city,
state: data.state,
description: data.description,
years_in_market: data.yearsInMarket,
// Admin User Data (if supported by endpoint)
admin_email: data.email,
admin_password: data.password, // Keep for backward compatibility if needed
password: data.password, // Correct field for CreateCompanyRequest
admin_name: data.companyName, // Or we add a contactPerson field
admin_birth_date: data.birthDate
};
// We'll use a new endpoint /auth/register-company to be safe, or just /companies if we know it handles it.
// Previously it was POST /companies with admin_email.
const res = await fetch(`${getApiV1Url()}/auth/register/company`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
console.error('[registerCompany] Error response:', res.status, errorData);
throw new Error(errorData.message || `Registration failed: ${res.status}`);
}
const responseData = await res.json().catch(() => ({}));
console.log('[registerCompany] Success - Company created:', responseData);
return responseData;
}