@@ -20,7 +20,8 @@ import { handleE2BRequestError } from '../../utils/errors'
2020export 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
0 commit comments