@@ -3,40 +3,40 @@ import { safeGetCurrentUser } from "@phala/cloud";
33import { defineCommand } from "@/src/core/define-command" ;
44import type { CommandContext } from "@/src/core/types" ;
55import { getClientWithKey } from "@/src/lib/client" ;
6- import { removeApiKey , saveApiKey } from "@/src/utils/credentials" ;
6+ import { DEFAULT_API_PREFIX , upsertProfile } from "@/src/utils/credentials" ;
77import { CLOUD_URL } from "@/src/utils/constants" ;
88
99import { logger } from "@/src/utils/logger" ;
10- import type { UserInfoResponse } from "@/src/api/types" ;
1110import { loginCommandMeta , loginCommandSchema } from "./command" ;
1211import type { LoginCommandInput } from "./command" ;
1312
14- async function validateAndPersistApiKey (
15- apiKey : string ,
16- ) : Promise < UserInfoResponse > {
17- try {
18- await saveApiKey ( apiKey ) ;
19- const client = await getClientWithKey ( apiKey ) ;
20- const result = await safeGetCurrentUser ( client ) ;
21- const userData = result . data as UserInfoResponse ;
22-
23- if ( ! result . success || ! userData ?. username ) {
24- await removeApiKey ( ) ;
25- throw new Error ( "Invalid API key" ) ;
26- }
13+ type CurrentUserInfo = {
14+ username : string ;
15+ email ?: string ;
16+ team_name ?: string ;
17+ } ;
2718
28- return userData ;
29- } catch ( error ) {
30- await removeApiKey ( ) ;
31- throw error instanceof Error ? error : new Error ( String ( error ) ) ;
19+ async function validateApiKey ( options : {
20+ apiKey : string ;
21+ baseURL : string ;
22+ } ) : Promise < CurrentUserInfo > {
23+ const client = await getClientWithKey ( options . apiKey , {
24+ baseURL : options . baseURL ,
25+ } ) ;
26+ const result = await safeGetCurrentUser ( client ) ;
27+ const userData = result . data as CurrentUserInfo ;
28+
29+ if ( ! result . success || ! userData ?. username ) {
30+ throw new Error ( "Invalid API key" ) ;
3231 }
32+
33+ return userData ;
3334}
3435
35- async function promptForApiKey ( ) : Promise < {
36- apiKey : string ;
37- user : UserInfoResponse ;
38- } > {
39- let cachedUser : UserInfoResponse | undefined ;
36+ async function promptForApiKey ( options : {
37+ baseURL : string ;
38+ } ) : Promise < { apiKey : string ; user : CurrentUserInfo } > {
39+ let cachedUser : CurrentUserInfo | undefined ;
4040 const response = await prompts ( {
4141 type : "password" ,
4242 name : "apiKey" ,
@@ -46,7 +46,10 @@ async function promptForApiKey(): Promise<{
4646 return "API key cannot be empty" ;
4747 }
4848 try {
49- cachedUser = await validateAndPersistApiKey ( value ) ;
49+ cachedUser = await validateApiKey ( {
50+ apiKey : value ,
51+ baseURL : options . baseURL ,
52+ } ) ;
5053 return true ;
5154 } catch ( error ) {
5255 return error instanceof Error ? error . message : "Invalid API key" ;
@@ -59,15 +62,18 @@ async function promptForApiKey(): Promise<{
5962 }
6063
6164 if ( ! cachedUser ) {
62- cachedUser = await validateAndPersistApiKey ( response . apiKey ) ;
65+ cachedUser = await validateApiKey ( {
66+ apiKey : response . apiKey ,
67+ baseURL : options . baseURL ,
68+ } ) ;
6369 }
6470
6571 return { apiKey : response . apiKey , user : cachedUser } ;
6672}
6773
6874async function runLoginCommand (
6975 input : LoginCommandInput ,
70- _context : CommandContext ,
76+ context : CommandContext ,
7177) : Promise < number > {
7278 // Show deprecation warning
7379 logger . warn (
@@ -77,23 +83,40 @@ async function runLoginCommand(
7783 logger . break ( ) ;
7884
7985 try {
86+ const baseURL = context . env . PHALA_CLOUD_API_PREFIX || DEFAULT_API_PREFIX ;
87+
8088 let apiKey = input . apiKey ;
81- let user : UserInfoResponse | undefined ;
89+ let user : CurrentUserInfo | undefined ;
8290
8391 if ( ! apiKey ) {
84- const result = await promptForApiKey ( ) ;
92+ const result = await promptForApiKey ( { baseURL } ) ;
8593 apiKey = result . apiKey ;
8694 user = result . user ;
8795 } else {
88- user = await validateAndPersistApiKey ( apiKey ) ;
96+ user = await validateApiKey ( { apiKey, baseURL } ) ;
8997 }
9098
9199 if ( ! user ) {
92100 throw new Error ( "Failed to validate API key" ) ;
93101 }
94102
103+ const workspaceName = user . team_name || "default" ;
104+ const profileName = workspaceName ;
105+
106+ upsertProfile ( {
107+ profileName,
108+ token : apiKey ,
109+ apiPrefix : baseURL ,
110+ workspaceName,
111+ user : {
112+ username : user . username ,
113+ email : user . email ,
114+ } ,
115+ setCurrent : true ,
116+ } ) ;
117+
95118 logger . success (
96- `Welcome ${ user . username } ! API key validated and saved successfully` ,
119+ `Welcome ${ user . username } ! Credentials saved successfully (profile: ${ profileName } ) ` ,
97120 ) ;
98121 logger . break ( ) ;
99122 logger . info ( `Open in Web UI at ${ CLOUD_URL } /dashboard/` ) ;
0 commit comments