feat: setup auth schema
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { createHttpClient } from '@repo/core-api/http-client';
|
||||
import { faroAdapter } from '@repo/core-api/observability';
|
||||
import { ENV } from '../environment';
|
||||
import { AppStorageKey, appStorage } from '../storage/local';
|
||||
import { AppDatabaseKey, AppStorageKey, appDatabase, appStorage } from '../storage/local';
|
||||
import { terminateAuthSession } from './auth.helper';
|
||||
|
||||
/**
|
||||
* Enterprise HTTP client for `apps/web`.
|
||||
@@ -32,7 +33,7 @@ export const apiClient = createHttpClient(
|
||||
const language = await appStorage.getItem<string>(AppStorageKey.LANGUAGE);
|
||||
config.headers['ex-language'] = language ?? ENV.DEFAULT_LANGUAGE;
|
||||
|
||||
const token = await appStorage.getItem<string>(AppStorageKey.ACCESS_TOKEN);
|
||||
const token = await appDatabase.getItem<string>(AppDatabaseKey.ACCESS_TOKEN);
|
||||
if (token) config.headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
return config;
|
||||
@@ -43,22 +44,7 @@ export const apiClient = createHttpClient(
|
||||
const status = error.response?.status;
|
||||
|
||||
// Catch 401 (Unauthorized) on request
|
||||
if (status === 401) {
|
||||
// Delete invalid tokens
|
||||
await appStorage.removeItem(AppStorageKey.ACCESS_TOKEN);
|
||||
|
||||
// Retrieve the current path and query string (example: /dashboard/settings?tab=profile)
|
||||
const currentPath = window.location.pathname + window.location.search;
|
||||
|
||||
// Retrieve the current path and query string (example: /dashboard/settings?tab=profile)
|
||||
const redirectParam = encodeURIComponent(currentPath);
|
||||
|
||||
// Use replace() instead of href.
|
||||
// replace() will not save the history of the page with this error,
|
||||
// so that if the user presses the 'back' button in the browser from the login page,
|
||||
// they won't get stuck in an infinite loop back to the login page.
|
||||
window.location.replace(`/auth/login?redirect=${redirectParam}`);
|
||||
}
|
||||
if (status === 401) if (status === 401) await terminateAuthSession({ preserveRedirect: true });
|
||||
|
||||
throw error;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user