refactor: centralize frontend auth query gating
This commit is contained in:
@@ -12,9 +12,27 @@ type AuthHandlers = {
|
||||
};
|
||||
|
||||
let auth: AuthHandlers | null = null;
|
||||
let hasAuthenticatedSession = false;
|
||||
const authSessionListeners = new Set<() => void>();
|
||||
|
||||
function setAuthenticatedSession(next: boolean) {
|
||||
if (next === hasAuthenticatedSession) return;
|
||||
hasAuthenticatedSession = next;
|
||||
for (const listener of authSessionListeners) listener();
|
||||
}
|
||||
|
||||
export function configureAuthClient(handlers: AuthHandlers | null) {
|
||||
auth = handlers;
|
||||
setAuthenticatedSession(Boolean(auth?.accessToken()));
|
||||
}
|
||||
|
||||
export function getAuthenticatedSessionSnapshot() {
|
||||
return hasAuthenticatedSession;
|
||||
}
|
||||
|
||||
export function subscribeAuthenticatedSession(listener: () => void) {
|
||||
authSessionListeners.add(listener);
|
||||
return () => authSessionListeners.delete(listener);
|
||||
}
|
||||
|
||||
async function authenticatedFetch(request: Request): Promise<Response> {
|
||||
|
||||
Reference in New Issue
Block a user