(options?: LoginOptions)
| 60 | }; |
| 61 | |
| 62 | export async function login(options?: LoginOptions): Promise<LoginResult> { |
| 63 | return await tracer.startActiveSpan("login", async (span) => { |
| 64 | try { |
| 65 | const opts = { defaultApiUrl: "https://api.trigger.dev", embedded: false, ...options }; |
| 66 | |
| 67 | span.setAttributes({ |
| 68 | "cli.config.apiUrl": opts.defaultApiUrl, |
| 69 | "cli.options.profile": opts.profile, |
| 70 | }); |
| 71 | |
| 72 | if (!opts.embedded) { |
| 73 | intro("Logging in to Trigger.dev"); |
| 74 | } |
| 75 | |
| 76 | const accessTokenFromEnv = process.env.TRIGGER_ACCESS_TOKEN; |
| 77 | |
| 78 | if (accessTokenFromEnv) { |
| 79 | const auth = { |
| 80 | accessToken: accessTokenFromEnv, |
| 81 | apiUrl: process.env.TRIGGER_API_URL ?? "https://api.trigger.dev", |
| 82 | }; |
| 83 | const apiClient = new CliApiClient(auth.apiUrl, auth.accessToken); |
| 84 | const userData = await apiClient.whoAmI(); |
| 85 | |
| 86 | if (!userData.success) { |
| 87 | throw new Error(userData.error); |
| 88 | } |
| 89 | |
| 90 | return { |
| 91 | ok: true as const, |
| 92 | profile: options?.profile ?? "default", |
| 93 | userId: userData.data.userId, |
| 94 | email: userData.data.email, |
| 95 | dashboardUrl: userData.data.dashboardUrl, |
| 96 | auth: { |
| 97 | accessToken: auth.accessToken, |
| 98 | apiUrl: auth.apiUrl, |
| 99 | }, |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | const authConfig = readAuthConfigProfile(options?.profile); |
| 104 | |
| 105 | if (authConfig && authConfig.accessToken) { |
| 106 | const whoAmIResult = await whoAmI( |
| 107 | { |
| 108 | profile: options?.profile ?? "default", |
| 109 | skipTelemetry: !span.isRecording(), |
| 110 | logLevel: logger.loggerLevel, |
| 111 | }, |
| 112 | true |
| 113 | ); |
| 114 | |
| 115 | if (!whoAmIResult.success) { |
| 116 | prettyError("Unable to validate existing personal access token", whoAmIResult.error); |
| 117 | |
| 118 | if (!opts.embedded) { |
| 119 | outro( |
no test coverage detected
searching dependent graphs…