@@ -28,13 +28,19 @@ export async function attemptLogin(
28
28
) {
29
29
apiBaseUrl ??= getConfigValueOrUndef ( 'apiBaseUrl' ) ?? undefined
30
30
apiProxy ??= getConfigValueOrUndef ( 'apiProxy' ) ?? undefined
31
- const apiToken =
32
- ( await password ( {
33
- message : `Enter your ${ terminalLink (
34
- 'Socket.dev API key' ,
35
- 'https://docs.socket.dev/docs/api-keys' ,
36
- ) } (leave blank for a public key)`,
37
- } ) ) || SOCKET_PUBLIC_API_TOKEN
31
+ const apiTokenInput = await password ( {
32
+ message : `Enter your ${ terminalLink (
33
+ 'Socket.dev API key' ,
34
+ 'https://docs.socket.dev/docs/api-keys' ,
35
+ ) } (leave blank for a public key)`,
36
+ } )
37
+
38
+ if ( apiTokenInput === undefined ) {
39
+ logger . fail ( 'Canceled by user' )
40
+ return { ok : false , message : 'Canceled' , cause : 'Canceled by user' }
41
+ }
42
+
43
+ const apiToken = apiTokenInput || SOCKET_PUBLIC_API_TOKEN
38
44
39
45
const sdk = await setupSdk ( apiToken , apiBaseUrl , apiProxy )
40
46
if ( ! sdk . ok ) {
@@ -54,10 +60,10 @@ export async function attemptLogin(
54
60
return
55
61
}
56
62
57
- logger . success ( 'API key verified' )
58
-
59
63
const orgs : SocketSdkReturnType < 'getOrganizations' > [ 'data' ] = result . data
60
64
65
+ logger . success ( `API key verified: ${ Object . values ( orgs . organizations ) } ` )
66
+
61
67
const enforcedChoices : OrgChoices = Object . values ( orgs . organizations )
62
68
. filter ( org => org ?. plan === 'enterprise' )
63
69
. map ( org => ( {
@@ -67,52 +73,61 @@ export async function attemptLogin(
67
73
68
74
let enforcedOrgs : string [ ] = [ ]
69
75
if ( enforcedChoices . length > 1 ) {
70
- const id = ( await select ( {
76
+ const id = await select ( {
71
77
message :
72
78
"Which organization's policies should Socket enforce system-wide?" ,
73
79
choices : enforcedChoices . concat ( {
74
80
name : 'None' ,
75
81
value : '' ,
76
82
description : 'Pick "None" if this is a personal device' ,
77
83
} ) ,
78
- } ) ) as string | null
84
+ } )
85
+ if ( id === undefined ) {
86
+ logger . fail ( 'Canceled by user' )
87
+ return { ok : false , message : 'Canceled' , cause : 'Canceled by user' }
88
+ }
79
89
if ( id ) {
80
90
enforcedOrgs = [ id ]
81
91
}
82
92
} else if ( enforcedChoices . length ) {
83
- if (
84
- await confirm ( {
85
- message : `Should Socket enforce ${ ( enforcedChoices [ 0 ] as OrgChoice ) ?. name } 's security policies system-wide?` ,
86
- default : true ,
87
- } )
88
- ) {
93
+ const shouldEnforce = await confirm ( {
94
+ message : `Should Socket enforce ${ ( enforcedChoices [ 0 ] as OrgChoice ) ?. name } 's security policies system-wide?` ,
95
+ default : true ,
96
+ } )
97
+ if ( shouldEnforce === undefined ) {
98
+ logger . fail ( 'Canceled by user' )
99
+ return { ok : false , message : 'Canceled' , cause : 'Canceled by user' }
100
+ }
101
+ if ( shouldEnforce ) {
89
102
const existing = enforcedChoices [ 0 ] as OrgChoice
90
103
if ( existing ) {
91
104
enforcedOrgs = [ existing . value ]
92
105
}
93
106
}
94
107
}
95
108
96
- if (
97
- isTestingV1 ( ) &&
98
- ( await select ( {
99
- message : 'Would you like to install bash tab completion?' ,
100
- choices : [
101
- {
102
- name : 'Yes' ,
103
- value : true ,
104
- description :
105
- 'Sets up tab completion for "socket" in your bash env. If you\'re unsure, this is probably what you want.' ,
106
- } ,
107
- {
108
- name : 'No' ,
109
- value : false ,
110
- description :
111
- 'Will skip tab completion setup. Does not change how Socket works.' ,
112
- } ,
113
- ] ,
114
- } ) )
115
- ) {
109
+ const wantToComplete = await select ( {
110
+ message : 'Would you like to install bash tab completion?' ,
111
+ choices : [
112
+ {
113
+ name : 'Yes' ,
114
+ value : true ,
115
+ description :
116
+ 'Sets up tab completion for "socket" in your bash env. If you\'re unsure, this is probably what you want.' ,
117
+ } ,
118
+ {
119
+ name : 'No' ,
120
+ value : false ,
121
+ description :
122
+ 'Will skip tab completion setup. Does not change how Socket works.' ,
123
+ } ,
124
+ ] ,
125
+ } )
126
+ if ( wantToComplete === undefined ) {
127
+ logger . fail ( 'Canceled by user' )
128
+ return { ok : false , message : 'Canceled' , cause : 'Canceled by user' }
129
+ }
130
+ if ( wantToComplete ) {
116
131
logger . log ( 'Setting up tab completion...' )
117
132
const result = await setupTabCompletion ( 'socket' )
118
133
if ( result . ok ) {
0 commit comments