mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-05 13:02:06 +00:00
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
|
|
/**
|
||
|
|
* Auth API contract (JWT):
|
||
|
|
* - POST Authentication/Login — returns token + user fields; persisted in localStorage
|
||
|
|
* - Session restored from stored login payload (no GetCurrentUser)
|
||
|
|
*/
|
||
|
|
import { API_PATHS } from "@/api/api-paths";
|
||
|
|
import { apiPost } from "@/api/api";
|
||
|
|
import { handleApiResponse } from "@/api/handle-api-response";
|
||
|
|
import { clearAuth, getAuthUser, isSessionValid, setAuthUser } from "@/lib/auth/auth-storage";
|
||
|
|
import type {
|
||
|
|
AuthUser,
|
||
|
|
ChangePasswordPayload,
|
||
|
|
LoginCredentials,
|
||
|
|
ResetPasswordPayload,
|
||
|
|
} from "@/domain/auth/types/auth-user";
|
||
|
|
|
||
|
|
export async function login(credentials: LoginCredentials): Promise<AuthUser> {
|
||
|
|
const response = await apiPost<unknown>(API_PATHS.authentication.login, credentials);
|
||
|
|
const user = handleApiResponse<AuthUser>(response);
|
||
|
|
setAuthUser(user);
|
||
|
|
return getAuthUser() ?? user;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function logout(): Promise<void> {
|
||
|
|
clearAuth();
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function getSession(): Promise<AuthUser | null> {
|
||
|
|
const user = getAuthUser();
|
||
|
|
|
||
|
|
if (!user || !isSessionValid(user)) {
|
||
|
|
clearAuth();
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return user;
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function changePassword(data: ChangePasswordPayload): Promise<unknown> {
|
||
|
|
return apiPost(API_PATHS.authentication.changePassword, data);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function forgotPassword(email: string): Promise<unknown> {
|
||
|
|
return apiPost(`${API_PATHS.authentication.forgetPassword}?Email=${encodeURIComponent(email)}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function verifyResetCode(code: string): Promise<unknown> {
|
||
|
|
return apiPost(`${API_PATHS.authentication.verificationCode}?code=${encodeURIComponent(code)}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function resetPassword(data: ResetPasswordPayload): Promise<unknown> {
|
||
|
|
return apiPost(API_PATHS.authentication.resetPassword, data);
|
||
|
|
}
|