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