mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-23 00:46:43 +02:00
Replace 900+ lines of custom auth implementation (authService, tokenManager, deviceManager, safeStorage) with ~280 lines wrapping @manacore/shared-auth. Auth now goes through mana-core-auth directly instead of manadeck backend. Backward-compatible API: all consumers (stores, apiClient, hooks) work without changes thanks to wrapper maintaining the same export interface. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
/**
|
|
* Token Manager - wraps @manacore/shared-auth TokenManager
|
|
* Maintains backward-compatible API for existing consumers
|
|
*/
|
|
|
|
import { _sharedTokenManager } from './authService';
|
|
export { TokenState } from '@manacore/shared-auth';
|
|
|
|
type TokenStateObserver = (state: string, token?: string | null) => void;
|
|
|
|
export const tokenManager = {
|
|
/**
|
|
* Get a valid access token, automatically refreshing if needed
|
|
*/
|
|
getValidToken: (): Promise<string | null> => {
|
|
return _sharedTokenManager.getValidToken();
|
|
},
|
|
|
|
/**
|
|
* Subscribe to token state changes
|
|
*/
|
|
subscribe: (callback: TokenStateObserver) => {
|
|
return _sharedTokenManager.subscribe(callback);
|
|
},
|
|
|
|
/**
|
|
* Clear all tokens (triggers sign-out)
|
|
*/
|
|
clearTokens: async (): Promise<void> => {
|
|
const { authService } = await import('./authService');
|
|
await authService.signOut();
|
|
},
|
|
|
|
/**
|
|
* Handle a 401 response by refreshing the token and retrying the request
|
|
*/
|
|
handle401Response: async (
|
|
input: string | URL | Request,
|
|
init?: RequestInit
|
|
): Promise<Response> => {
|
|
const token = await _sharedTokenManager.getValidToken();
|
|
if (!token) {
|
|
throw new Error('Session expired. Please sign in again.');
|
|
}
|
|
|
|
const headers = new Headers(init?.headers);
|
|
headers.set('Authorization', `Bearer ${token}`);
|
|
|
|
return fetch(input, { ...init, headers });
|
|
},
|
|
};
|