Checkpoint 2

This commit is contained in:
Tuan Dang
2023-02-13 17:11:31 +07:00
parent 30b2b85446
commit 13b1805d04
3 changed files with 13 additions and 1 deletions

View File

@@ -1,7 +1,11 @@
import { getAuthToken, setAuthToken } from '@app/reactQuery';
import { getAuthToken, setAuthToken , setMfaTempToken } from '@app/reactQuery';
// depreciated: go for apiRequest module in config/api
export default class SecurityClient {
static setMfaToken(tokenStr: string) {
setMfaTempToken(tokenStr);
}
static setToken(tokenStr: string) {
setAuthToken(tokenStr);
}

View File

@@ -11,6 +11,9 @@ export const apiRequest = axios.create({
apiRequest.interceptors.request.use((config) => {
const token = getAuthToken();
console.log('interceptors');
console.log('token', token);
console.log('config.headers', config.headers);
if (token && config.headers) {
// eslint-disable-next-line no-param-reassign
config.headers.Authorization = `Bearer ${token}`;

View File

@@ -1,6 +1,7 @@
import { QueryClient } from '@tanstack/react-query';
// this is saved in react-query cache
export const MFA_TEMP_TOKEN_CACHE_KEY = ['infisical__mfa-temp-token'];
export const AUTH_TOKEN_CACHE_KEY = ['infisical__auth-token'];
export const queryClient = new QueryClient({
@@ -13,9 +14,13 @@ export const queryClient = new QueryClient({
});
// set token in memory cache
export const setMfaTempToken = (token: string) =>
queryClient.setQueryData(MFA_TEMP_TOKEN_CACHE_KEY, token);
export const setAuthToken = (token: string) =>
queryClient.setQueryData(AUTH_TOKEN_CACHE_KEY, token);
export const getMfaTempToken = () => queryClient.getQueryData(MFA_TEMP_TOKEN_CACHE_KEY) as string;
export const getAuthToken = () => queryClient.getQueryData(AUTH_TOKEN_CACHE_KEY) as string;
export const isLoggedIn = () => Boolean(getAuthToken());