Some checks failed
ci/woodpecker/push/ci Pipeline failed
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
30 lines
782 B
TypeScript
30 lines
782 B
TypeScript
import type { AuthResult } from '../auth.js';
|
|
|
|
export interface AuthContext {
|
|
gateway: string;
|
|
session: AuthResult;
|
|
cookie: string;
|
|
}
|
|
|
|
/**
|
|
* Load and validate the user's auth session.
|
|
* Exits with an error message if not signed in or session expired.
|
|
*/
|
|
export async function withAuth(gateway: string): Promise<AuthContext> {
|
|
const { loadSession, validateSession } = await import('../auth.js');
|
|
|
|
const session = loadSession(gateway);
|
|
if (!session) {
|
|
console.error('Not signed in. Run `mosaic login` first.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const valid = await validateSession(gateway, session.cookie);
|
|
if (!valid) {
|
|
console.error('Session expired. Run `mosaic login` again.');
|
|
process.exit(1);
|
|
}
|
|
|
|
return { gateway, session, cookie: session.cookie };
|
|
}
|