Skip to content

Commit 10db4e9

Browse files
CLI: Improve auth login typing + browser sign in response usage (#733)
This pr improves the auth login code, to be more declarative and realistic to the actual response that is sent by the dashboard sign in flow.
1 parent 231e196 commit 10db4e9

4 files changed

Lines changed: 42 additions & 13 deletions

File tree

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"prepublishOnly": "pnpm build",
3939
"build": "tsc --noEmit --skipLibCheck && tsup --minify",
4040
"dev": "tsup --watch",
41+
"lint": "eslint src",
4142
"test": "pnpm build && cd testground/demo-basic && ../../dist/index.js template build",
4243
"check-deps": "knip",
4344
"update-deps": "ncu -u && pnpm i",

packages/cli/src/commands/auth/login.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { handleE2BRequestError } from '../../utils/errors'
2020
export const loginCommand = new commander.Command('login')
2121
.description('log in to CLI')
2222
.action(async () => {
23-
let userConfig
23+
let userConfig: UserConfig | null = null
24+
2425
try {
2526
userConfig = getUserConfig()
2627
} catch (err) {
@@ -35,16 +36,18 @@ export const loginCommand = new commander.Command('login')
3536
return
3637
} else if (!userConfig) {
3738
console.log('Attempting to log in...')
38-
userConfig = await signInWithBrowser()
39-
if (!userConfig) {
39+
const signInResponse = await signInWithBrowser()
40+
if (!signInResponse) {
4041
console.info('Login aborted')
4142
return
4243
}
4344

45+
const accessToken =
46+
process.env.E2B_ACCESS_TOKEN || signInResponse.accessToken
47+
4448
const signal = connectionConfig.getSignal()
4549
const config = new e2b.ConnectionConfig({
46-
accessToken: process.env.E2B_ACCESS_TOKEN || userConfig?.accessToken,
47-
apiKey: process.env.E2B_API_KEY || userConfig?.teamApiKey,
50+
accessToken,
4851
})
4952
const client = new e2b.ApiClient(config)
5053
const res = await client.api.GET('/teams', { signal })
@@ -61,9 +64,14 @@ export const loginCommand = new commander.Command('login')
6164
process.exit(1)
6265
}
6366

64-
userConfig.teamName = defaultTeam.name
65-
userConfig.teamId = defaultTeam.teamID
66-
userConfig.teamApiKey = defaultTeam.apiKey
67+
userConfig = {
68+
email: signInResponse.email,
69+
accessToken,
70+
teamName: defaultTeam.name,
71+
teamId: defaultTeam.teamID,
72+
teamApiKey: defaultTeam.apiKey,
73+
}
74+
6775
fs.mkdirSync(path.dirname(USER_CONFIG_PATH), { recursive: true })
6876
fs.writeFileSync(USER_CONFIG_PATH, JSON.stringify(userConfig, null, 2))
6977
}
@@ -76,7 +84,13 @@ export const loginCommand = new commander.Command('login')
7684
process.exit(0)
7785
})
7886

79-
async function signInWithBrowser(): Promise<UserConfig> {
87+
interface SignInWithBrowserResponse {
88+
email: string
89+
accessToken: string
90+
defaultTeamId: string
91+
}
92+
93+
async function signInWithBrowser(): Promise<SignInWithBrowserResponse> {
8094
const server = http.createServer()
8195
const { port } = await listen.default(server, 0, '127.0.0.1')
8296
const loginUrl = new URL(`${DOCS_BASE}/api/cli`)
@@ -92,7 +106,7 @@ async function signInWithBrowser(): Promise<UserConfig> {
92106
.searchParams
93107
const searchParamsObj = Object.fromEntries(
94108
searchParams.entries()
95-
) as unknown as UserConfig & {
109+
) as unknown as SignInWithBrowserResponse & {
96110
error?: string
97111
}
98112
const { error } = searchParamsObj

packages/cli/src/user.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@ import * as os from 'os'
22
import * as path from 'path'
33
import * as fs from 'fs'
44

5+
/**
6+
* User configuration stored in ~/.e2b/config.json
7+
*/
58
export interface UserConfig {
69
email: string
710
accessToken: string
8-
defaultTeamApiKey?: string
9-
defaultTeamId?: string
1011
teamName: string
1112
teamId: string
1213
teamApiKey: string
1314
dockerProxySet?: boolean
15+
16+
/**
17+
* @deprecated Kept for backward compatibility. Use {@link UserConfig.teamApiKey} instead.
18+
*/
19+
defaultTeamApiKey?: string
20+
21+
/**
22+
* @deprecated Kept for backward compatibility. Use {@link UserConfig.teamId} instead.
23+
*/
24+
defaultTeamId?: string
1425
}
1526

1627
export const USER_CONFIG_PATH = path.join(os.homedir(), '.e2b', 'config.json') // TODO: Keep in Keychain
17-
export const DOCS_BASE = process.env.E2B_DOCS_BASE || `https://${process.env.E2B_DOMAIN || 'e2b.dev'}/docs`
28+
export const DOCS_BASE =
29+
process.env.E2B_DOCS_BASE ||
30+
`https://${process.env.E2B_DOMAIN || 'e2b.dev'}/docs`
1831

1932
export function getUserConfig(): UserConfig | null {
2033
if (!fs.existsSync(USER_CONFIG_PATH)) return null

packages/js-sdk/src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ApiClient {
3434
requireApiKey?: boolean
3535
} = { requireAccessToken: false, requireApiKey: true }
3636
) {
37+
// FIXME: This statement makes no sense
3738
if (!opts?.requireApiKey && !config.apiKey) {
3839
throw new AuthenticationError(
3940
'API key is required, please visit the Team tab at https://e2b.dev/dashboard to get your API key. ' +

0 commit comments

Comments
 (0)