Skip to content

Commit

Permalink
fix(SSR): manage token
Browse files Browse the repository at this point in the history
  • Loading branch information
emile-bex committed Jun 3, 2021
1 parent a7d6c53 commit 34841fc
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/core/api/interceptors.ts
Original file line number Diff line number Diff line change
@@ -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<string | null> {
// 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),
Expand Down

0 comments on commit 34841fc

Please sign in to comment.