xpeditis2.0/apps/frontend/src/lib/api/auth.ts
2026-06-12 11:33:37 +02:00

101 lines
2.5 KiB
TypeScript

/**
* Authentication API
*
* Endpoints for user authentication and session management
*/
import { get, post, clearAuthTokens } from './client';
import type {
RegisterRequest,
LoginRequest,
AuthResponse,
RefreshTokenRequest,
UserPayload,
SuccessResponse,
} from '@/types/api';
/**
* Register new user
* POST /api/v1/auth/register
*/
export async function register(data: RegisterRequest): Promise<AuthResponse> {
// Tokens are delivered as httpOnly cookies by the backend
return post<AuthResponse>('/api/v1/auth/register', data, false);
}
/**
* User login
* POST /api/v1/auth/login
*/
export async function login(data: LoginRequest & { rememberMe?: boolean }): Promise<AuthResponse> {
// Tokens are delivered as httpOnly cookies by the backend;
// rememberMe drives cookie persistence server-side
return post<AuthResponse>('/api/v1/auth/login', data, false);
}
/**
* Refresh access token
* POST /api/v1/auth/refresh
*/
export async function refreshToken(data?: RefreshTokenRequest): Promise<{ success: boolean }> {
// The backend reads the httpOnly refresh cookie and rotates the cookies
return post<{ success: boolean }>('/api/v1/auth/refresh', data ?? {}, false);
}
/**
* Logout
* POST /api/v1/auth/logout
*/
export async function logout(): Promise<SuccessResponse> {
try {
const response = await post<SuccessResponse>('/api/v1/auth/logout');
return response;
} finally {
// Always clear tokens locally
clearAuthTokens();
}
}
/**
* Get current user profile
* GET /api/v1/auth/me
*/
export async function getCurrentUser(): Promise<UserPayload> {
return get<UserPayload>('/api/v1/auth/me');
}
/**
* Contact form — send message to contact@xpeditis.com
* POST /api/v1/auth/contact
*/
export async function sendContactForm(data: {
firstName: string;
lastName: string;
email: string;
company?: string;
phone?: string;
subject: string;
message: string;
}): Promise<{ message: string }> {
return post<{ message: string }>('/api/v1/auth/contact', data, false);
}
/**
* Forgot password — request reset email
* POST /api/v1/auth/forgot-password
*/
export async function forgotPassword(email: string): Promise<{ message: string }> {
return post<{ message: string }>('/api/v1/auth/forgot-password', { email }, false);
}
/**
* Reset password with token from email
* POST /api/v1/auth/reset-password
*/
export async function resetPassword(
token: string,
newPassword: string
): Promise<{ message: string }> {
return post<{ message: string }>('/api/v1/auth/reset-password', { token, newPassword }, false);
}