Files
Account/src/lib/authToken.ts
Koosha Lahouti fb9d691f6a feat: add otp api
2025-08-12 02:51:35 +03:30

56 lines
1.3 KiB
TypeScript

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 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 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> {
await sendEmailOtp(email);
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;
}