feat: add otp api

This commit is contained in:
Koosha Lahouti
2025-08-06 11:16:31 -07:00
parent 3e23fae993
commit fb9d691f6a
3 changed files with 108 additions and 51 deletions

View File

@@ -1,33 +1,54 @@
// src/lib/authService.ts
import axios from 'axios';
export interface SendEmailOtpResponse {
success: boolean;
errorCode: number;
message: string;
validations: {
message: string;
code: number;
property: string;
severity: number;
}[];
}
export interface TokenResponse {
access_token: string;
expires_in: number;
refresh_token: string;
}
const authClient = axios.create({
baseURL: 'https://account.business-harmony.com',
timeout: 10000,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});
const SEND_EMAIL_OTP_URL =
'https://account.business-harmony.com/api/User/SendEmailOtp';
const TOKEN_URL = 'https://account.business-harmony.com/connect/token';
export async function loginWithPassword(
username: string,
password: string,
export async function sendEmailOtp(
email: string,
): Promise<SendEmailOtpResponse> {
const { data } = await axios.post<SendEmailOtpResponse>(SEND_EMAIL_OTP_URL, {
email,
});
return data;
}
export async function fetchAuthToken(
email: string,
otpCode: string,
): Promise<TokenResponse> {
const body = new URLSearchParams();
body.set('grant_type', 'password');
body.set('username', username);
body.set('password', password);
body.set('client_id', 'harmony_identity');
body.set('scope', 'openid harmony_identity profile offline_access');
await sendEmailOtp(email);
const { data } = await authClient.post<TokenResponse>(
'/connect/token',
body.toString(),
);
const body = new URLSearchParams({
grant_type: 'otp',
client_id: 'harmony_identity',
phonenumber: '',
email,
otp_code: otpCode,
scope: 'openid profile offline_access harmony_identity',
}).toString();
const { data } = await axios.post<TokenResponse>(TOKEN_URL, body, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
});
localStorage.setItem('authToken', data.access_token);
return data;