whoami.ts54 lines · main
1import { apiCall, ApiCallError } from '../api-client.js';
2import { readCredentials } from '../config.js';
3import { readProjectConfig } from '../project-config.js';
4import { banner, blankLine, error as printError, step, success } from '../output.js';
5
6interface InfoResponse {
7 projectId: string;
8 authenticatedVia: 'api_key' | 'session';
9 apiKeyId: string | null;
10 userId: string | null;
11}
12
13export async function runWhoami(): Promise<number> {
14 const file = await readCredentials();
15 const local = await readProjectConfig();
16
17 const targetId = local?.projectId ?? file.default;
18 if (!targetId) {
19 banner('whoami');
20 blankLine();
21 step('no linked project found.');
22 step('run: briven connect && briven projects use <p_...>');
23 step(' or: briven login --project <p_...> --key <brk_...>');
24 return 1;
25 }
26
27 const cred = file.projects[targetId];
28 if (!cred) {
29 printError(`no credentials stored for ${targetId}`);
30 step('run: briven login --project <id> --key <brk_...>');
31 return 1;
32 }
33
34 try {
35 const info = await apiCall<InfoResponse>(`/v1/projects/${cred.projectId}/info`, {
36 apiOrigin: cred.apiOrigin,
37 apiKey: cred.apiKey,
38 });
39 banner('whoami');
40 step(`project ${info.projectId}`);
41 step(`origin ${cred.apiOrigin}`);
42 step(`key suffix ${cred.suffix}`);
43 step(`auth ${info.authenticatedVia}`);
44 success('credentials ok');
45 return 0;
46 } catch (err) {
47 if (err instanceof ApiCallError) {
48 printError(`server rejected: ${err.code} (${err.status})`);
49 } else {
50 printError(err instanceof Error ? err.message : 'unknown error');
51 }
52 return 1;
53 }
54}