Accounts 1.7.1, Website 1.0.2, Events 3.7.1: link right after an install; Preview behind the proxy
Found installing the Website for cross-demo2. - All three: a connect involving an install that isn't in the cached copy of the Hub's list yet asks the Hub for a fresh list (one heartbeat, at most once a minute) instead of refusing. A new Website's admin couldn't sign in for up to 30 minutes. - Accounts: a heartbeat on every start. - Website: /admin links to Accounts on the spot (at most every 15 s), one connect to Accounts at a time, and the problem page says "not linked yet" (ACCOUNTS_NOT_LINKED). Preview redirected to the server's own http://localhost:<web port>: the preview routes now answer with a relative Location. - Events: a new Website can connect for the event feed straight away. - docs: VERSIONS, changelogs, WHATS-NEW. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,13 +8,15 @@ import type * as AuthClient from '@crosscode/auth-client';
|
||||
import type { AccountsClient } from '@crosscode/auth-client';
|
||||
import type { Ctx } from '../context.js';
|
||||
import { ApiError } from '../http.js';
|
||||
import { linkAccountsNow } from '../links/connect.js';
|
||||
|
||||
export type AuthClientModule = typeof AuthClient;
|
||||
|
||||
export const loadAuthClient = (): Promise<AuthClientModule> => import('@crosscode/auth-client');
|
||||
|
||||
export class AccountsUnavailable extends ApiError {
|
||||
constructor(message: string) {
|
||||
/** 1.0.2: `not_linked` (no link to Accounts yet) or `unreachable` (linked, but it didn't answer). */
|
||||
constructor(message: string, public reason: 'not_linked' | 'unreachable' = 'unreachable') {
|
||||
super(503, 'ACCOUNTS_UNAVAILABLE', message);
|
||||
}
|
||||
}
|
||||
@@ -28,10 +30,15 @@ const cache = new WeakMap<Ctx, { key: string; client: Promise<AccountsClient> }>
|
||||
|
||||
/** The client for the church's Accounts (discovery done once per link). Throws AccountsUnavailable. */
|
||||
export async function accounts(ctx: Ctx): Promise<AccountsClient> {
|
||||
const link = await accountsLink(ctx);
|
||||
if (!link) throw new AccountsUnavailable('The Website isn\'t linked to the church\'s Accounts yet. It links by itself within 15 minutes of being installed.');
|
||||
let link = await accountsLink(ctx);
|
||||
if (!link) {
|
||||
// 1.0.2: link now rather than wait for the next heartbeat.
|
||||
await linkAccountsNow(ctx);
|
||||
link = await accountsLink(ctx);
|
||||
}
|
||||
if (!link) throw new AccountsUnavailable('The Website isn\'t linked to the church\'s Accounts yet.', 'not_linked');
|
||||
const clientId = await ctx.settings.get('hub_install_id');
|
||||
if (!clientId) throw new AccountsUnavailable('The Website isn\'t connected to the Hub yet.');
|
||||
if (!clientId) throw new AccountsUnavailable('The Website isn\'t connected to the Hub yet.', 'not_linked');
|
||||
const key = `${link.installId}|${link.updatedAt.getTime()}|${clientId}|${ctx.config.publicUrl}`;
|
||||
let hit = cache.get(ctx);
|
||||
if (!hit || hit.key !== key) {
|
||||
|
||||
@@ -34,6 +34,9 @@ const page = (ctx: Ctx, path: string) => `${ctx.config.publicUrl}${path}`;
|
||||
const problem = (ctx: Ctx, res: Response, code: string, returnTo = '/admin') =>
|
||||
res.redirect(303, page(ctx, `/auth/problem?code=${encodeURIComponent(code)}&returnTo=${encodeURIComponent(safeReturnPath(returnTo))}`));
|
||||
|
||||
/** 1.0.2: the problem page says which it is: not linked to Accounts yet, or Accounts not answering. */
|
||||
const unavailableCode = (e: AccountsUnavailable) => (e.reason === 'not_linked' ? 'ACCOUNTS_NOT_LINKED' : 'ACCOUNTS_UNAVAILABLE');
|
||||
|
||||
type UserRow = { id: string; name: string; email: string; role: WebsiteRole };
|
||||
|
||||
/**
|
||||
@@ -70,7 +73,7 @@ export function authRouter(ctx: Ctx) {
|
||||
else res.clearCookie(MFA_COOKIE, cookieOptions(ctx, COOKIE_PATH));
|
||||
return res.redirect(303, url);
|
||||
} catch (e) {
|
||||
if (e instanceof AccountsUnavailable) return problem(ctx, res, 'ACCOUNTS_UNAVAILABLE', returnTo);
|
||||
if (e instanceof AccountsUnavailable) return problem(ctx, res, unavailableCode(e), returnTo);
|
||||
console.error('[auth] Starting a sign-in failed:', e);
|
||||
return problem(ctx, res, 'SIGNIN_FAILED', returnTo);
|
||||
}
|
||||
@@ -86,7 +89,7 @@ export function authRouter(ctx: Ctx) {
|
||||
const auth = await accounts(ctx);
|
||||
result = await auth.finishSignIn(new URL(req.originalUrl, ctx.config.publicUrl), cookies[PENDING_COOKIE]);
|
||||
} catch (e) {
|
||||
if (e instanceof AccountsUnavailable) return problem(ctx, res, 'ACCOUNTS_UNAVAILABLE');
|
||||
if (e instanceof AccountsUnavailable) return problem(ctx, res, unavailableCode(e));
|
||||
const code = (e as { code?: string }).code;
|
||||
// The person pressed Cancel at Accounts, or the pending cookie expired.
|
||||
if (req.query.error === 'access_denied') return res.redirect(303, page(ctx, '/'));
|
||||
|
||||
Reference in New Issue
Block a user