Skip to content

Commit

Permalink
Set only auth headers in token.ts DAH-1071 (#1589)
Browse files Browse the repository at this point in the history
  • Loading branch information
akegan authored Feb 17, 2022
1 parent a65bfbf commit 587592a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/javascript/api/apiService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"

import { setHeaders, getHeaders } from "../authentication/token"
import { setAuthHeaders, getHeaders } from "../authentication/token"

// Use this function for authenticated calls
const createAxiosInstance = (): AxiosInstance => {
Expand All @@ -12,7 +12,7 @@ const createAxiosInstance = (): AxiosInstance => {
headers: getHeaders(),
transformResponse: (res, headers) => {
if (headers["access-token"]) {
setHeaders(headers)
setAuthHeaders(headers)
}
return JSON.parse(res)
},
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/api/authApiService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setHeaders } from "../authentication/token"
import { setAuthHeaders } from "../authentication/token"
import { User, UserData } from "../authentication/user"
import { authenticatedGet, post, put } from "./apiService"

Expand All @@ -7,7 +7,7 @@ export const signIn = async (email: string, password: string): Promise<User> =>
email,
password,
}).then(({ data, headers }) => {
setHeaders(headers)
setAuthHeaders(headers)
return data.data
})

Expand Down
12 changes: 10 additions & 2 deletions app/javascript/authentication/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ export interface AuthHeaders {
"token-type": string
}

export const setHeaders = (headers: AuthHeaders) => {
getStorage().setItem(ACCESS_TOKEN_LOCAL_STORAGE_KEY, JSON.stringify(headers))
export const setAuthHeaders = (headers: AuthHeaders) => {
// Set only relevant auth headers
const headersToSet = {
expiry: headers.expiry,
"access-token": headers["access-token"],
client: headers.client,
uid: headers.uid,
"token-type": headers["token-type"],
}
getStorage().setItem(ACCESS_TOKEN_LOCAL_STORAGE_KEY, JSON.stringify(headersToSet))
}

export const getHeaders = (): AuthHeaders | undefined => getAuthHeaders()
Expand Down

0 comments on commit 587592a

Please sign in to comment.