@@ -179,31 +179,32 @@ export class Commands {
179179 }
180180
181181 /**
182- * Log into the provided deployment. If the deployment URL is not specified,
182+ * Log into the provided deployment. If the deployment URL is not specified,
183183 * ask for it first with a menu showing recent URLs along with the default URL
184184 * and CODER_URL, if those are set.
185185 */
186- public async login ( ...args : string [ ] ) : Promise < void > {
187- // Destructure would be nice but VS Code can pass undefined which errors.
188- const inputUrl = args [ 0 ] ;
189- const inputToken = args [ 1 ] ;
190- const inputLabel = args [ 2 ] ;
191- const isAutologin =
192- typeof args [ 3 ] === "undefined" ? false : Boolean ( args [ 3 ] ) ;
193-
194- const url = await this . maybeAskUrl ( inputUrl ) ;
186+ public async login ( args ?: {
187+ url ?: string ;
188+ token ?: string ;
189+ label ?: string ;
190+ autoLogin ?: boolean ;
191+ } ) : Promise < void > {
192+ const url = await this . maybeAskUrl ( args ?. url ) ;
195193 if ( ! url ) {
196194 return ; // The user aborted.
197195 }
198196
199197 // It is possible that we are trying to log into an old-style host, in which
200198 // case we want to write with the provided blank label instead of generating
201199 // a host label.
202- const label =
203- typeof inputLabel === "undefined" ? toSafeHost ( url ) : inputLabel ;
200+ const label = args ?. label === undefined ? toSafeHost ( url ) : args ?. label ;
204201
205202 // Try to get a token from the user, if we need one, and their user.
206- const res = await this . maybeAskToken ( url , inputToken , isAutologin ) ;
203+ const res = await this . maybeAskToken (
204+ url ,
205+ args ?. token ,
206+ args ?. autoLogin === true ,
207+ ) ;
207208 if ( ! res ) {
208209 return ; // The user aborted, or unable to auth.
209210 }
@@ -257,21 +258,23 @@ export class Commands {
257258 */
258259 private async maybeAskToken (
259260 url : string ,
260- token : string ,
261- isAutologin : boolean ,
261+ token : string | undefined ,
262+ isAutoLogin : boolean ,
262263 ) : Promise < { user : User ; token : string } | null > {
263264 const client = CoderApi . create ( url , token , this . logger , ( ) =>
264265 vscode . workspace . getConfiguration ( ) ,
265266 ) ;
266- if ( ! needToken ( vscode . workspace . getConfiguration ( ) ) ) {
267- try {
268- const user = await client . getAuthenticatedUser ( ) ;
269- // For non-token auth, we write a blank token since the `vscodessh`
270- // command currently always requires a token file.
271- return { token : "" , user } ;
272- } catch ( err ) {
267+ const needsToken = needToken ( vscode . workspace . getConfiguration ( ) ) ;
268+ try {
269+ const user = await client . getAuthenticatedUser ( ) ;
270+ // For non-token auth, we write a blank token since the `vscodessh`
271+ // command currently always requires a token file.
272+ // For token auth, we have valid access so we can just return the user here
273+ return { token : needsToken && token ? token : "" , user } ;
274+ } catch ( err ) {
275+ if ( ! needToken ( vscode . workspace . getConfiguration ( ) ) ) {
273276 const message = getErrorMessage ( err , "no response from the server" ) ;
274- if ( isAutologin ) {
277+ if ( isAutoLogin ) {
275278 this . logger . warn ( "Failed to log in to Coder server:" , message ) ;
276279 } else {
277280 this . vscodeProposed . window . showErrorMessage (
@@ -303,6 +306,9 @@ export class Commands {
303306 value : token || ( await this . secretsManager . getSessionToken ( ) ) ,
304307 ignoreFocusOut : true ,
305308 validateInput : async ( value ) => {
309+ if ( ! value ) {
310+ return null ;
311+ }
306312 client . setSessionToken ( value ) ;
307313 try {
308314 user = await client . getAuthenticatedUser ( ) ;
@@ -371,7 +377,10 @@ export class Commands {
371377 // Sanity check; command should not be available if no url.
372378 throw new Error ( "You are not logged in" ) ;
373379 }
380+ await this . forceLogout ( ) ;
381+ }
374382
383+ public async forceLogout ( ) : Promise < void > {
375384 // Clear from the REST client. An empty url will indicate to other parts of
376385 // the code that we are logged out.
377386 this . restClient . setHost ( "" ) ;
@@ -390,7 +399,7 @@ export class Commands {
390399 . showInformationMessage ( "You've been logged out of Coder!" , "Login" )
391400 . then ( ( action ) => {
392401 if ( action === "Login" ) {
393- vscode . commands . executeCommand ( "coder. login" ) ;
402+ this . login ( ) ;
394403 }
395404 } ) ;
396405
0 commit comments