Files
Stirling-PDF/frontend/shared/auth/spring/platformBridge.ts
T
James Brunton 1816bad1ba Unified auth for portal and editor (#6725)
# Description of Changes
Refactor frontend auth to the shared folder and hook it up to both the
portal and editor so they share the same system. Also adds various tasks
to help run the portal, including `task dev:portal` to spawn the portal
with the backend, and `task dev:portal:proxy` to spawn the editor,
portal and backend, and a reverse proxy (at localhost:3000) to allow you
to use both at once to simulate how this will actually be deployed,
allowing you to check whether the seamless transition between the two
actually works.
2026-06-22 16:22:36 +00:00

71 lines
2.5 KiB
TypeScript

/**
* Platform seam for the Spring auth client.
*
* The client itself is platform-agnostic: it never inspects the JWT directly
* or touches Tauri/desktop storage. Each host wires its own behaviour through
* this bridge. The web default (used by the portal and the editor's web builds)
* is a no-op set that mirrors the editor's previous `@app/extensions/*`
* defaults exactly; the editor's desktop build injects a Tauri-backed bridge.
*/
/**
* Resolved identity for the current session, as understood by the platform
* layer that owns the underlying token format. Treated as opaque by the client.
*/
export interface PlatformSessionUser {
username: string;
email?: string;
/** True for anonymous/guest sessions (e.g. Supabase anonymous sign-in). */
is_anonymous?: boolean;
}
export interface PlatformBridge {
/** Clear platform-specific cached auth after sign-out (e.g. Tauri store). */
clearPlatformAuthAfterSignOut(): Promise<void>;
/** Clear platform-specific cached auth when the login page initialises. */
clearPlatformAuthOnLoginInit(): Promise<void>;
/** Whether the active backend is a desktop SaaS gateway (Supabase-managed). */
isDesktopSaaSAuthMode(): Promise<boolean>;
/** Whether the active backend exposes /api/v1/auth/logout. */
shouldCallBackendLogout(): Promise<boolean>;
/** Resolve the current user from platform storage (desktop only). */
getPlatformSessionUser(): Promise<PlatformSessionUser | null>;
/** Refresh the session through the platform layer (desktop only). */
refreshPlatformSession(): Promise<boolean>;
/** Persist the token to platform-specific storage (Tauri store). */
savePlatformToken(token: string): Promise<void>;
/** Begin an OAuth navigation; return true if the platform handled it. */
startOAuthNavigation(redirectUrl: string): Promise<boolean>;
}
/**
* Web defaults - byte-for-byte equivalent to the editor's previous
* proprietary/extensions defaults so web behaviour is unchanged.
*/
export const defaultPlatformBridge: PlatformBridge = {
async clearPlatformAuthAfterSignOut() {
// no-op for web builds
},
async clearPlatformAuthOnLoginInit() {
// no-op for web builds
},
async isDesktopSaaSAuthMode() {
return false;
},
async shouldCallBackendLogout() {
return true;
},
async getPlatformSessionUser() {
return null;
},
async refreshPlatformSession() {
return false;
},
async savePlatformToken() {
// Web mode: token already saved to localStorage in the auth client.
},
async startOAuthNavigation() {
return false;
},
};