update usermanagement to legacy mode

This commit is contained in:
mengstabketemaw
2026-06-18 11:25:26 +03:00
parent 3a528c9515
commit 2feaaf1ee4
13 changed files with 158 additions and 57 deletions

View File

@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import { authStorage } from '@ema-platform/auth';
/**
* Same-origin host for the user-management module.
@@ -18,18 +19,13 @@ import { useNavigate, useLocation } from 'react-router-dom';
*/
function readToken(): string | null {
return (
localStorage.getItem('ema-backoffice-auth-token') ??
(() => {
const escaped = 'auth-token'.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1');
const match = document.cookie.match(new RegExp('(?:^|; )' + escaped + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
})()
);
const escaped = 'auth-token'.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1');
const match = document.cookie.match(new RegExp('(?:^|; )' + escaped + '=([^;]*)'));
return authStorage.getToken() ?? (match ? decodeURIComponent(match[1]) : null);
}
function readRefreshToken(): string | null {
return localStorage.getItem('ema-backoffice-refresh-token') ?? null;
return authStorage.getRefreshToken() ?? null;
}
export default function UserManagementHostPage() {

View File

@@ -1,13 +1 @@
import { Navigate, Outlet } from 'react-router-dom';
function getTokenFromCookie(): string | undefined {
const match = document.cookie.match(/(?:^|;\s*)auth-token=([^;]*)/);
return match ? decodeURIComponent(match[1]) : undefined;
}
export function ProtectedRoute() {
const token =
localStorage.getItem('ema-backoffice-auth-token') ?? getTokenFromCookie();
if (!token) return <Navigate to="/login" replace />;
return <Outlet />;
}
export { ProtectedRoute } from '@ema-platform/auth';

View File

@@ -1,6 +1,13 @@
import { configureStore } from '@reduxjs/toolkit';
import { baseApi } from '@ema-platform/api';
import { authReducer, signupReducer, configureAuthStorage, authStorage } from '@ema-platform/auth';
import { baseApi, configureTokenRefresh } from '@ema-platform/api';
import {
authReducer,
signupReducer,
configureAuthStorage,
authStorage,
refreshAccessToken,
logout,
} from '@ema-platform/auth';
import type { AuthUser } from '@ema-platform/auth';
configureAuthStorage('ema-backoffice');
@@ -25,5 +32,13 @@ export const store = configureStore({
getDefaultMiddleware().concat(baseApi.middleware),
});
configureTokenRefresh({
onTokenExpired: refreshAccessToken,
onAuthFailure: () => {
store.dispatch(logout());
window.location.href = '/login';
},
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

View File

@@ -1,13 +1 @@
import { Navigate, useLocation } from 'react-router-dom';
import { useAppSelector } from '../store/hooks';
export function ProtectedRoute({ children }: { children: React.ReactNode }) {
const isAuthenticated = useAppSelector((s) => s.auth.isAuthenticated);
const location = useLocation();
if (!isAuthenticated) {
return <Navigate to="/login" state={{ from: location }} replace />;
}
return <>{children}</>;
}
export { ProtectedRoute } from '@ema-platform/auth';

View File

@@ -1,6 +1,13 @@
import { configureStore } from '@reduxjs/toolkit';
import { baseApi } from '@ema-platform/api';
import { authReducer, signupReducer, configureAuthStorage, authStorage } from '@ema-platform/auth';
import { baseApi, configureTokenRefresh } from '@ema-platform/api';
import {
authReducer,
signupReducer,
configureAuthStorage,
authStorage,
refreshAccessToken,
logout,
} from '@ema-platform/auth';
import type { AuthUser } from '@ema-platform/auth';
configureAuthStorage('ema-portal');
@@ -25,5 +32,13 @@ export const store = configureStore({
getDefaultMiddleware().concat(baseApi.middleware),
});
configureTokenRefresh({
onTokenExpired: refreshAccessToken,
onAuthFailure: () => {
store.dispatch(logout());
window.location.href = '/login';
},
});
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;