feat: implement web URL constants and enhance navigation handling
- Introduced a centralized `WEB_URL` constant for managing application routes, improving maintainability and readability across components. - Updated various components, including login, auth, and main app modules, to utilize the new `WEB_URL` constants for navigation, ensuring consistency in route management. - Added a new `AppHomeRedirect` component to streamline user redirection based on privileges, enhancing user experience. - Implemented client-side navigation functions to prevent full page reloads, improving performance and user interaction. - Added tests for new functionalities, ensuring reliability in navigation and URL handling. These changes significantly enhance the application's routing structure and navigation efficiency, providing a more cohesive user experience.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { appDatabase, AppDatabaseKey, appStorage, AppStorageKey } from '../storage/local';
|
||||
import { API_URL } from '../constants/api-url';
|
||||
import { WEB_URL } from '../constants/web-url';
|
||||
import { clientReplace, notifySessionEnd, notifySessionStart } from './client-navigation';
|
||||
import { mapUserPrivileges } from './map-user-privileges';
|
||||
import { safeInternalPath } from './safe-internal-path';
|
||||
import type { AuthUser, TokenPair } from './auth.types';
|
||||
|
||||
interface TerminateOptions {
|
||||
@@ -39,16 +42,17 @@ export async function terminateAuthSession(options: TerminateOptions = {}): Prom
|
||||
await appDatabase.removeItem(AppDatabaseKey.REFRESH_TOKEN);
|
||||
await appStorage.removeItem(AppStorageKey.USER_ID);
|
||||
|
||||
let loginUrl = '/auth/login';
|
||||
const isNotLoginPage = !window.location.pathname.includes('/auth/login');
|
||||
let loginUrl = WEB_URL.LOGIN;
|
||||
const isLoginPage =
|
||||
window.location.pathname === WEB_URL.LOGIN || window.location.pathname.startsWith(`${WEB_URL.LOGIN}/`);
|
||||
|
||||
if (isNotLoginPage) {
|
||||
if (!isLoginPage) {
|
||||
if (preserveRedirect) {
|
||||
const currentPath = window.location.pathname + window.location.search;
|
||||
loginUrl += `?redirect=${encodeURIComponent(currentPath)}`;
|
||||
loginUrl += `?redirect=${encodeURIComponent(window.location.pathname)}`;
|
||||
}
|
||||
|
||||
window.location.replace(loginUrl);
|
||||
notifySessionEnd();
|
||||
clientReplace(loginUrl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,10 +63,10 @@ export async function initiateAuthSession(tokens: TokenPair, me: AuthUser): Prom
|
||||
|
||||
await persistTokenPair(tokens);
|
||||
await persistUserSession(me);
|
||||
notifySessionStart();
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const redirectTo = params.get('redirect');
|
||||
window.location.replace(redirectTo ? decodeURIComponent(redirectTo) : '/app');
|
||||
clientReplace(safeInternalPath(params.get('redirect'), WEB_URL.APP));
|
||||
}
|
||||
|
||||
let refreshInFlight: Promise<TokenPair> | null = null;
|
||||
|
||||
Reference in New Issue
Block a user