mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-26 13:02:50 +00:00
feat: update session management to prioritize cookie storage and enhance token retrieval logic
This commit is contained in:
10
README.md
10
README.md
@@ -37,8 +37,8 @@ emaui/
|
||||
```
|
||||
|
||||
### libs/api
|
||||
- `base-api/` — RTK Query `createApi` instance with `prepareHeaders` that injects the Bearer token from Redux state or localStorage.
|
||||
- `session/` — `resolveTokenFromStorage()` reads from `localStorage` keys or `auth-token` cookie. `resolveSessionContext()` merges Redux state token with storage fallback.
|
||||
- `base-api/` — RTK Query `createApi` instance with `prepareHeaders` that injects the Bearer token from Redux state or storage.
|
||||
- `session/` — `resolveTokenFromStorage()` reads the `auth-token` cookie first, falling back to `localStorage` for legacy pre-migration sessions. `resolveSessionContext()` merges Redux state token with storage fallback.
|
||||
- `query-and-mutation/` — Generic `useApiQuery` / `useApiMutation` wrappers for one-off API calls without defining a dedicated endpoint file.
|
||||
|
||||
### libs/ui
|
||||
@@ -55,10 +55,10 @@ emaui/
|
||||
|
||||
1. User submits the login form (LoginForm / LoginPage).
|
||||
2. The form calls the `login` RTK Query mutation (backoffice) or a plain `fetch` (portal).
|
||||
3. On success, `loginSuccess` action is dispatched → Redux `auth` slice stores `token` and `user`; `authStorage.setToken()` persists the token to `localStorage`.
|
||||
3. On success, `loginSuccess` action is dispatched → Redux `auth` slice stores `token` and `user`; `authStorage.setToken()` persists the token to a cookie (both apps call `configureAuthStorage(prefix, true)`).
|
||||
4. `baseApi`'s `prepareHeaders` reads the token via `resolveSessionContext(getState())` and attaches `Authorization: Bearer <token>` to every RTK Query request.
|
||||
5. `ProtectedRoute` checks `localStorage` for the token key on every navigation — if absent, redirects to `/login`.
|
||||
6. `logout` action clears Redux state and calls `authStorage.clear()` to remove all localStorage keys.
|
||||
5. `ProtectedRoute` checks `authStorage`/the token cookie on every navigation — if absent, redirects to `/login`.
|
||||
6. `logout` action clears Redux state and calls `authStorage.clear()` to remove all auth cookies.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
} from "@ema-platform/auth";
|
||||
import type { AuthUser, CurrentProfile } from "@ema-platform/auth";
|
||||
|
||||
configureAuthStorage("ema-portal");
|
||||
configureAuthStorage("ema-portal", true);
|
||||
|
||||
const preloadedAuth = (() => {
|
||||
const token = authStorage.getToken();
|
||||
|
||||
@@ -14,7 +14,7 @@ const TOKEN_STORAGE_KEYS = [
|
||||
] as const;
|
||||
|
||||
export function resolveTokenFromStorage(): string | undefined {
|
||||
// cookie first (backoffice stores tokens there), then localStorage (portal / legacy)
|
||||
// cookie first, then localStorage (legacy pre-migration sessions)
|
||||
for (const key of TOKEN_STORAGE_KEYS) {
|
||||
const cookie = Cookies.get(key);
|
||||
if (cookie) return cookie;
|
||||
|
||||
Reference in New Issue
Block a user