diff --git a/src/core/api/interceptors.ts b/src/core/api/interceptors.ts index d3e5f138..58bb2982 100644 --- a/src/core/api/interceptors.ts +++ b/src/core/api/interceptors.ts @@ -1,23 +1,25 @@ import { AxiosInstance } from 'axios' import humps from 'humps' import { env } from 'src/core/env' -import { getTokenFromCookies } from 'src/core/services' +import { createAnonymousUser, getTokenFromCookies } from 'src/core/services' import { notifServerError } from 'src/utils/misc' export function addAxiosInterceptors(client: AxiosInstance) { - function getUserToken(): string | null { + async function getUserToken(): Promise { // TODO: improve with token cache in memory for browser side - return getTokenFromCookies() + return getTokenFromCookies() || createAnonymousUser() } - client.interceptors.request.use((request) => { + client.interceptors.request.use(async (request) => { const { params = {} } = request + const token = params.token || await getUserToken() + return { ...request, params: { ...humps.decamelizeKeys(request.params || {}), - token: params.token || getUserToken(), + token, api_key: env.API_KEY, // eslint-disable-line }, data: request.data && humps.decamelizeKeys(request.data),