forked from mosaicstack/stack
Co-authored-by: Jason Woltje <[email protected]> Co-committed-by: Jason Woltje <[email protected]>
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 };
|
|
}
|