Skip to content

Commit f2d274f

Browse files
committed
Detect ctrl-c as cancel during interactive login
1 parent f7bf1f9 commit f2d274f

File tree

1 file changed

+52
-37
lines changed

1 file changed

+52
-37
lines changed

src/commands/login/attempt-login.mts

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ export async function attemptLogin(
2828
) {
2929
apiBaseUrl ??= getConfigValueOrUndef('apiBaseUrl') ?? undefined
3030
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
3844

3945
const sdk = await setupSdk(apiToken, apiBaseUrl, apiProxy)
4046
if (!sdk.ok) {
@@ -54,10 +60,10 @@ export async function attemptLogin(
5460
return
5561
}
5662

57-
logger.success('API key verified')
58-
5963
const orgs: SocketSdkReturnType<'getOrganizations'>['data'] = result.data
6064

65+
logger.success(`API key verified: ${Object.values(orgs.organizations)}`)
66+
6167
const enforcedChoices: OrgChoices = Object.values(orgs.organizations)
6268
.filter(org => org?.plan === 'enterprise')
6369
.map(org => ({
@@ -67,52 +73,61 @@ export async function attemptLogin(
6773

6874
let enforcedOrgs: string[] = []
6975
if (enforcedChoices.length > 1) {
70-
const id = (await select({
76+
const id = await select({
7177
message:
7278
"Which organization's policies should Socket enforce system-wide?",
7379
choices: enforcedChoices.concat({
7480
name: 'None',
7581
value: '',
7682
description: 'Pick "None" if this is a personal device',
7783
}),
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+
}
7989
if (id) {
8090
enforcedOrgs = [id]
8191
}
8292
} 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) {
89102
const existing = enforcedChoices[0] as OrgChoice
90103
if (existing) {
91104
enforcedOrgs = [existing.value]
92105
}
93106
}
94107
}
95108

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) {
116131
logger.log('Setting up tab completion...')
117132
const result = await setupTabCompletion('socket')
118133
if (result.ok) {

0 commit comments

Comments
 (0)