Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/commands/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,12 @@ export const cloudCommand = defineCommand({

const auth = await resolveAuth({ apiKeyFlag });

// Nudge interactive api-key users toward `dcd login`, which unlocks live
// (realtime) status updates. Suppressed in CI and non-interactive output.
// Nudge interactive api-key users toward `dcd login`. Suppressed in CI and
// non-interactive output.
if (auth.mode === 'apiKey' && !json && !quiet && !isCI()) {
out(
colors.dim(
'Tip: run `dcd login` for live test updates and a smoother experience than passing --api-key.',
'Tip: run `dcd login` for a smoother experience than passing --api-key.',
),
);
}
Expand Down
36 changes: 4 additions & 32 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ import { defineCommand } from 'citty';
import { createHash, randomBytes } from 'node:crypto';

import {
type DcdEnvName,
ENVIRONMENTS,
inferEnvFromApiUrl,
resolveFrontendUrl,
} from '../config/environments.js';
import { getSupabaseKey } from '../config/supabase-key.js';
import { jsonFlag } from '../config/flags/output.flags.js';
import { openBrowser } from '../utils/browser.js';
import { canPrompt } from '../utils/ci.js';
Expand Down Expand Up @@ -201,17 +199,11 @@ export const loginCommand = defineCommand({
try {
orgs = await fetchOrgs(apiUrl, bearerHeaders);
} catch (err) {
// The api rejected the JWT. Before blaming the api, hit Supabase's
// own /auth/v1/user directly with the same token — if Supabase also
// rejects it, the token itself is bad; if Supabase accepts, the
// api's JwtService is misconfigured.
// The api rejected the JWT it just minted. Name the token's issuer, so
// a token from the wrong environment is plain from the error.
const iss = decodeJwtIssuer(payload.access_token);
const supabaseUser = await probeSupabaseUser(iss, env, payload.access_token);
const parts = [
iss ? `token iss: ${iss}` : null,
`supabase /auth/v1/user: ${supabaseUser}`,
].filter(Boolean);
throw new CliError(`${(err as Error).message} [${parts.join(' | ')}]`);
const message = (err as Error).message;
throw new CliError(iss ? `${message} [token iss: ${iss}]` : message);
}
const { org: chosen, fromLastLogin } = await selectOrg(orgs, {
requested: requestedOrg,
Expand Down Expand Up @@ -319,26 +311,6 @@ function base64url(buf: Buffer): string {
return buf.toString('base64').replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
}

async function probeSupabaseUser(
iss: string | null,
env: DcdEnvName,
token: string,
): Promise<string> {
if (!iss) return 'skipped (no iss)';
try {
const res = await fetch(`${iss}/user`, {
headers: {
authorization: `Bearer ${token}`,
apikey: await getSupabaseKey(env),
},
});
const bodyText = await res.text().catch(() => '');
return `HTTP ${res.status}${bodyText ? ` — ${bodyText.slice(0, 200)}` : ''}`;
} catch (err) {
return `network error: ${(err as Error).message}`;
}
}

function decodeJwtIssuer(token: string): string | null {
try {
const [, payloadB64] = token.split('.');
Expand Down
7 changes: 3 additions & 4 deletions src/commands/logout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* `dcd logout` — clears the stored Supabase session and best-effort revokes
* it on Supabase. Leaves DEVICE_CLOUD_API_KEY (env/flag) untouched.
* `dcd logout` — deletes the stored session. Local only: nothing is revoked
* server-side, so the user stays signed in to the web app and any other CLI.
* Leaves DEVICE_CLOUD_API_KEY (env/flag) untouched.
*/
import { defineCommand } from 'citty';

import { jsonFlag } from '../config/flags/output.flags.js';
import { CliAuthGateway } from '../gateways/cli-auth-gateway.js';
import { logger } from '../utils/cli.js';
import { clearConfig, getConfigPath, readConfig } from '../utils/config-store.js';
import { colors } from '../utils/styling.js';
Expand All @@ -32,7 +32,6 @@ export const logoutCommand = defineCommand({
return;
}

await CliAuthGateway.signOut(config.supabase_url, config.env, config.session);
clearConfig();
if (json) {
logger.json({ status: 'SUCCESS', loggedOut: true, user: config.session.user_email });
Expand Down
22 changes: 0 additions & 22 deletions src/gateways/cli-auth-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,4 @@ export const CliAuthGateway = {
user_id: u.id,
};
},

async signOut(
supabaseUrl: string,
env: DcdEnvName,
session: StoredSession,
): Promise<void> {
// `scope: 'local'` clears this client's SDK state only — no server call,
// no revocation. supabase-js defaults to `scope: 'global'`, which revokes
// EVERY session for the user (browser, mobile, other CLIs), so
// `dcd logout` would kick the user out of the web app. We don't want
// that — logging out locally should be local.
try {
const sb = client(supabaseUrl, await getSupabaseKey(env));
await sb.auth.setSession({
access_token: session.access_token,
refresh_token: session.refresh_token,
});
await sb.auth.signOut({ scope: 'local' });
} catch {
// Best effort — the local config will be wiped regardless.
}
},
};
183 changes: 0 additions & 183 deletions src/gateways/realtime-gateway.ts

This file was deleted.

Loading
Loading