import _ from 'lodash' import { AxiosError } from 'axios' import OrderappClientApi from './index' export default class AuthApi { api: OrderappClientApi constructor (api: OrderappClientApi) { this.api = api this.api.axiosGo.interceptors.response.use( data => data, async (error: AxiosError) => { error = this.sanitizeError(error) console.log('[Axios][Interceptor] Response Error', error) this.api.logError(error) const res = error.response const errorMessage: string = res?.data?.error ?? '' // customer api token expired error if (res?.status === 401 && errorMessage.includes('error.token.expired')) { // customer token expired if (this.api.refreshCustomerApiCallback != null) { try { await this.api.refreshCustomerApiCallback() // retry with new token error.config.headers.Authorization = this.api.axiosGo.defaults.headers.common.Authorization return await this.api.axiosGo.request(error.config) } catch (error) { return await Promise.reject(error) } } return } // orderapp token expired error if (res?.status === 406 && errorMessage === 'token expired') { if (this.api.refresh != null) { const user = await this.refresh(this.api.refresh) if (this.api.updateUserCallback != null) { await this.api.updateUserCallback(user) } // retry with new token error.config.headers.Authorization = this.api.axiosGo.defaults.headers.common.Authorization return await this.api.axiosGo.request(error.config) } } // other 401 error if (res?.status === 401) { if (this.api.resetUserCallback != null) { this.api.resetUserCallback(errorMessage ?? res?.status) return } } return await Promise.reject(error) }, ) this.api.axiosNode.interceptors.response.use( data => data, async (error: AxiosError) => { error = this.sanitizeError(error) console.log('[Axios][Interceptor] Response Error', error) this.api.logError(error) const res = error.response const errorMessage: string = res?.data?.message ?? '' if (res?.status === 500 && errorMessage === 'UnauthorizedError: jwt expired') { if (this.api.refresh != null) { const user = await this.refresh(this.api.refresh) error.config.headers.Authorization = this.api.axiosNode.defaults.headers.common.Authorization if (this.api.updateUserCallback != null) { this.api.updateUserCallback(user) } // retry return await this.api.axiosNode.request(error.config) } } else if (res?.status === 401) { if (this.api.resetUserCallback != null) { this.api.resetUserCallback(res?.data?.error ?? errorMessage ?? res?.status) return } } return await Promise.reject(error) }, ) } sanitizeError (error: AxiosError): AxiosError { try { const data = JSON.parse(error.config.data) if (_.get(data, 'payment.payload.paymentMethod.paymentCard') != null) { delete data.payment.payload.paymentMethod.paymentCard } error.config.data = JSON.stringify(data) } catch (e) { } return error } async scopeLogin (merchantUsername: string, username: string, password: string): Promise { const endpoint = '/c/user/scopelogin' const data = { merchantUsername, username, password, } const response = await this.api.axiosGo.post(endpoint, data) this.api.setToken(response.data.token, response.data.refresh) return response.data } async register (payload: string = '', hash: string = '', client: string = ''): Promise { const endpoint = '/c/user/register' const data = { payload, hash, client } const response: {data: IRegisterResponse} = await this.api.axiosGo.post(endpoint, data) this.api.setToken(response.data.token, response.data.refresh) return response.data } async refresh (token: string): Promise { const endpoint = '/c/user/refresh' const response = await this.api.axiosGo.post(endpoint, { token: token, }) this.api.setToken(response.data.token, response.data.refresh) return response.data } async createCustomer (): Promise<{id: string}> { const endpoint = '/customer' const response = await this.api.axiosGo.post(endpoint) return response.data } }