@@ -166,31 +166,32 @@ export class Commands {
166
166
}
167
167
168
168
/**
169
- * Log into the provided deployment. If the deployment URL is not specified,
169
+ * Log into the provided deployment. If the deployment URL is not specified,
170
170
* ask for it first with a menu showing recent URLs along with the default URL
171
171
* and CODER_URL, if those are set.
172
172
*/
173
- public async login ( ...args : string [ ] ) : Promise < void > {
174
- // Destructure would be nice but VS Code can pass undefined which errors.
175
- const inputUrl = args [ 0 ] ;
176
- const inputToken = args [ 1 ] ;
177
- const inputLabel = args [ 2 ] ;
178
- const isAutologin =
179
- typeof args [ 3 ] === "undefined" ? false : Boolean ( args [ 3 ] ) ;
180
-
181
- const url = await this . maybeAskUrl ( inputUrl ) ;
173
+ public async login ( args ?: {
174
+ url ?: string ;
175
+ token ?: string ;
176
+ label ?: string ;
177
+ autoLogin ?: boolean ;
178
+ } ) : Promise < void > {
179
+ const url = await this . maybeAskUrl ( args ?. url ) ;
182
180
if ( ! url ) {
183
181
return ; // The user aborted.
184
182
}
185
183
186
184
// It is possible that we are trying to log into an old-style host, in which
187
185
// case we want to write with the provided blank label instead of generating
188
186
// a host label.
189
- const label =
190
- typeof inputLabel === "undefined" ? toSafeHost ( url ) : inputLabel ;
187
+ const label = args ?. label === undefined ? toSafeHost ( url ) : args ?. label ;
191
188
192
189
// Try to get a token from the user, if we need one, and their user.
193
- const res = await this . maybeAskToken ( url , inputToken , isAutologin ) ;
190
+ const res = await this . maybeAskToken (
191
+ url ,
192
+ args ?. token ,
193
+ args ?. autoLogin === true ,
194
+ ) ;
194
195
if ( ! res ) {
195
196
return ; // The user aborted, or unable to auth.
196
197
}
@@ -244,21 +245,23 @@ export class Commands {
244
245
*/
245
246
private async maybeAskToken (
246
247
url : string ,
247
- token : string ,
248
- isAutologin : boolean ,
248
+ token : string | undefined ,
249
+ isAutoLogin : boolean ,
249
250
) : Promise < { user : User ; token : string } | null > {
250
251
const client = CoderApi . create ( url , token , this . logger , ( ) =>
251
252
vscode . workspace . getConfiguration ( ) ,
252
253
) ;
253
- if ( ! needToken ( vscode . workspace . getConfiguration ( ) ) ) {
254
- try {
255
- const user = await client . getAuthenticatedUser ( ) ;
256
- // For non-token auth, we write a blank token since the `vscodessh`
257
- // command currently always requires a token file.
258
- return { token : "" , user } ;
259
- } catch ( err ) {
254
+ const needsToken = needToken ( vscode . workspace . getConfiguration ( ) ) ;
255
+ try {
256
+ const user = await client . getAuthenticatedUser ( ) ;
257
+ // For non-token auth, we write a blank token since the `vscodessh`
258
+ // command currently always requires a token file.
259
+ // For token auth, we have valid access so we can just return the user here
260
+ return { token : needsToken && token ? token : "" , user } ;
261
+ } catch ( err ) {
262
+ if ( ! needToken ( vscode . workspace . getConfiguration ( ) ) ) {
260
263
const message = getErrorMessage ( err , "no response from the server" ) ;
261
- if ( isAutologin ) {
264
+ if ( isAutoLogin ) {
262
265
this . logger . warn ( "Failed to log in to Coder server:" , message ) ;
263
266
} else {
264
267
this . vscodeProposed . window . showErrorMessage (
@@ -290,6 +293,9 @@ export class Commands {
290
293
value : token || ( await this . secretsManager . getSessionToken ( ) ) ,
291
294
ignoreFocusOut : true ,
292
295
validateInput : async ( value ) => {
296
+ if ( ! value ) {
297
+ return null ;
298
+ }
293
299
client . setSessionToken ( value ) ;
294
300
try {
295
301
user = await client . getAuthenticatedUser ( ) ;
@@ -358,7 +364,10 @@ export class Commands {
358
364
// Sanity check; command should not be available if no url.
359
365
throw new Error ( "You are not logged in" ) ;
360
366
}
367
+ await this . forceLogout ( ) ;
368
+ }
361
369
370
+ public async forceLogout ( ) : Promise < void > {
362
371
// Clear from the REST client. An empty url will indicate to other parts of
363
372
// the code that we are logged out.
364
373
this . restClient . setHost ( "" ) ;
@@ -377,7 +386,7 @@ export class Commands {
377
386
. showInformationMessage ( "You've been logged out of Coder!" , "Login" )
378
387
. then ( ( action ) => {
379
388
if ( action === "Login" ) {
380
- vscode . commands . executeCommand ( "coder. login" ) ;
389
+ this . login ( ) ;
381
390
}
382
391
} ) ;
383
392
0 commit comments