import { AxiosError } from 'axios' import OrderappMerchantApi from './index' export default class AuthApi { api: OrderappMerchantApi constructor (api: OrderappMerchantApi) { this.api = api this.api.axiosGo.interceptors.response.use( data => data, async (error: AxiosError) => { this.api.logError(error) console.log('[Axios][Interceptor] Response Error', error) const res = error.response if (res?.status === 406 && res?.data?.error === 'token expired') { if (this.api.refresh != null) { if (this.api.updateUserCallback != null) { const user = await this.refresh(this.api.refresh) this.api.updateUserCallback(user) error.config.headers.Authorization = this.api.axiosGo.defaults.headers.common.Authorization return await this.api.axiosGo.request(error.config) } } } else if (res?.status === 401) { if (this.api.resetUserCallback != null) { this.api.resetUserCallback(res?.data?.error ?? res?.status) return } } return await Promise.reject(error) }, ) this.api.axiosNode.interceptors.response.use( data => data, async (error: AxiosError) => { this.api.logError(error) console.log('[Axios][Interceptor] Response Error', error) const res = error.response if (res?.status === 500 && res?.data?.message === 'UnauthorizedError: jwt expired') { if (this.api.refresh != null) { if (this.api.updateUserCallback != null) { const user = await this.refresh(this.api.refresh) this.api.updateUserCallback(user) error.config.headers.Authorization = this.api.axiosNode.defaults.headers.common.Authorization return await this.api.axiosNode.request(error.config) } } } else if (res?.status === 401) { if (this.api.resetUserCallback != null) { this.api.resetUserCallback(res?.data?.error ?? res?.data?.message ?? res?.status) return } } return await Promise.reject(error) }, ) } async login (username: string, password: string): Promise { const endpoint = '/m/user/login' const response: {data: ILoginData} = await this.api.axiosGo.post(endpoint, { username, password }) this.api.setToken(response.data.token, response.data.refresh) return response.data } async scopeLogin (username: string, password: string): Promise { const endpoint = '/m/user/scopelogin' const response = await this.api.axiosGo.post(endpoint, { username, password }) this.api.setToken(response.data.token, response.data.refresh) return response.data } async refresh (token: string): Promise { const endpoint = '/m/user/refresh' const response: {data: ILoginData} = await this.api.axiosGo.post(endpoint, { token }) this.api.setToken(response.data.token, response.data.refresh) return response.data } // 建立 user 或更改密碼 async updateUser (username: string, password: string): Promise { const response = await this.api.axiosGo.post('/m/user/cred', { username, password }) return response.data } // 刪除 user async deleteUser (username: string): Promise { const response = await this.api.axiosGo.delete(`/m/user/cred/${username}`) return response.data } // 更改 user 權限 async updateUserScope (username: string, scopes: TScope[]): Promise { const response = await this.api.axiosGo.post('/m/user/cred/scope', { username, scopes }) return response.data } }