146 lines
3.7 KiB
TypeScript

import BaseApiService from "@Front/Api/BaseApiService";
import { ICustomerTokens } from "../Id360/Customers/Customers";
import { TotpCodesReasons } from "le-coffre-resources/dist/Customer/TotpCodes";
export type IMailVerifyParams = {
email: string;
};
export type IMailVerifyReturn = {
partialPhoneNumber: string;
totpCodeUid: string;
};
export type IVerifyTotpCodeParams = {
totpCode: string;
email: string;
};
export type IVerifyTotpCodeReturn = {
validCode: boolean;
reason: TotpCodesReasons;
};
export type ISetPasswordParams = {
password: string;
email: string;
totpCode: string;
};
export type ILoginParams = {
password: string;
email: string;
totpCode: string;
};
export type IAskNewPasswordParams = {
email: string;
};
export type IAskAnotherCodeParams = {
email: string;
totpCodeUid: string;
};
export type IAskAnotherCodeReturn = {
partialPhoneNumber: string;
totpCodeUid: string;
};
export default class Auth extends BaseApiService {
private static instance: Auth;
protected readonly namespaceUrl = this.getBaseUrl().concat("/customer");
private readonly baseURl = this.namespaceUrl.concat("/auth");
public static getInstance() {
return (this.instance ??= new this());
}
public async mailVerifySms(body: IMailVerifyParams): Promise<IMailVerifyReturn> {
const url = new URL(this.baseURl.concat("/mail/verify-sms"));
try {
return this.postRequest<IMailVerifyReturn>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async verifyTotpCode(body: IVerifyTotpCodeParams): Promise<IVerifyTotpCodeReturn> {
const url = new URL(this.baseURl.concat("/verify-totp-code"));
try {
return this.postRequest<IVerifyTotpCodeReturn>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async setPassword(body: ISetPasswordParams): Promise<ICustomerTokens> {
const url = new URL(this.baseURl.concat("/set-password"));
try {
return this.postRequest<ICustomerTokens>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async login(body: ILoginParams): Promise<ICustomerTokens> {
const url = new URL(this.baseURl.concat("/login"));
try {
return this.postRequest<ICustomerTokens>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async askNewPassword(body: IAskNewPasswordParams): Promise<IMailVerifyReturn> {
const url = new URL(this.baseURl.concat("/ask-new-password"));
try {
return this.postRequest<IMailVerifyReturn>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async sendAnotherCode(body: IAskAnotherCodeParams): Promise<IAskAnotherCodeReturn> {
const url = new URL(this.baseURl.concat("/send-another-code"));
try {
return this.postRequest<IAskAnotherCodeReturn>(url, body);
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
public async clientAuth(body: IClientAuthParams): Promise<IClientAuthReturn> {
// Construct the full URL for the client-auth endpoint
// This endpoint is at /api/v1/client-auth, not part of the customer auth namespace
const url = new URL(this.baseURl.concat("/client-auth"));
try {
// Create custom headers for this specific endpoint
const headers = new Headers();
headers.set("Content-Type", "application/json");
headers.set("x-session-id", body.sessionId);
const response = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ pairingId: body.pairingId })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (err) {
this.onError(err);
return Promise.reject(err);
}
}
}