(profile: string = "default")
| 27 | }; |
| 28 | |
| 29 | export async function isLoggedIn(profile: string = "default"): Promise<LoginResult> { |
| 30 | return await tracer.startActiveSpan("isLoggedIn", async (span) => { |
| 31 | try { |
| 32 | const config = readAuthConfigProfile(profile); |
| 33 | |
| 34 | if (!config?.accessToken || !config?.apiUrl) { |
| 35 | span.recordException(new Error("You must login first")); |
| 36 | span.end(); |
| 37 | return { ok: false as const, error: "You must login first" }; |
| 38 | } |
| 39 | |
| 40 | const apiClient = new CliApiClient(config.apiUrl, config.accessToken); |
| 41 | const userData = await apiClient.whoAmI(); |
| 42 | |
| 43 | if (!userData.success) { |
| 44 | recordSpanException(span, userData.error); |
| 45 | span.end(); |
| 46 | |
| 47 | return { |
| 48 | ok: false as const, |
| 49 | error: userData.error, |
| 50 | auth: { |
| 51 | apiUrl: config.apiUrl, |
| 52 | accessToken: config.accessToken, |
| 53 | }, |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | span.setAttributes({ |
| 58 | "login.userId": userData.data.userId, |
| 59 | "login.email": userData.data.email, |
| 60 | "login.dashboardUrl": userData.data.dashboardUrl, |
| 61 | "login.profile": profile, |
| 62 | }); |
| 63 | |
| 64 | span.end(); |
| 65 | |
| 66 | return { |
| 67 | ok: true as const, |
| 68 | profile, |
| 69 | userId: userData.data.userId, |
| 70 | email: userData.data.email, |
| 71 | dashboardUrl: userData.data.dashboardUrl, |
| 72 | auth: { |
| 73 | apiUrl: config.apiUrl, |
| 74 | accessToken: config.accessToken, |
| 75 | }, |
| 76 | }; |
| 77 | } catch (e) { |
| 78 | recordSpanException(span, e); |
| 79 | span.end(); |
| 80 | |
| 81 | return { |
| 82 | ok: false as const, |
| 83 | error: e instanceof Error ? e.message : "Unknown error", |
| 84 | }; |
| 85 | } |
| 86 | }); |
no test coverage detected
searching dependent graphs…