@@ -159,31 +159,32 @@ export class Commands{
159159}
160160
161161/**
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,
163163 * ask for it first with a menu showing recent URLs along with the default URL
164164 * and CODER_URL, if those are set.
165165 */
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 ) ;
175173if ( ! url ) {
176174return ; // The user aborted.
177175}
178176
179177// It is possible that we are trying to log into an old-style host, in which
180178// case we want to write with the provided blank label instead of generating
181179// a host label.
182- const label =
183- typeof inputLabel === "undefined" ? toSafeHost ( url ) : inputLabel ;
180+ const label = args ?. label === undefined ? toSafeHost ( url ) : args ?. label ;
184181
185182// 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+ ) ;
187188if ( ! res ) {
188189return ; // The user aborted, or unable to auth.
189190}
@@ -237,21 +238,23 @@ export class Commands{
237238 */
238239private async maybeAskToken (
239240url : string ,
240- token : string ,
241- isAutologin : boolean ,
241+ token : string | undefined ,
242+ isAutoLogin : boolean ,
242243) : Promise < { user : User ; token : string } | null > {
243244const client = CoderApi . create ( url , token , this . storage . output , ( ) =>
244245vscode . workspace . getConfiguration ( ) ,
245246) ;
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 ( ) ) ) {
253256const message = getErrorMessage ( err , "no response from the server" ) ;
254- if ( isAutologin ) {
257+ if ( isAutoLogin ) {
255258this . storage . output . warn (
256259"Failed to log in to Coder server:" ,
257260message ,
@@ -286,6 +289,9 @@ export class Commands{
286289value : token || ( await this . storage . getSessionToken ( ) ) ,
287290ignoreFocusOut : true ,
288291validateInput : async ( value ) => {
292+ if ( ! value ) {
293+ return null ;
294+ }
289295client . setSessionToken ( value ) ;
290296try {
291297user = await client . getAuthenticatedUser ( ) ;
@@ -354,7 +360,10 @@ export class Commands{
354360// Sanity check; command should not be available if no url.
355361throw new Error ( "You are not logged in" ) ;
356362}
363+ await this . forceLogout ( ) ;
364+ }
357365
366+ public async forceLogout ( ) : Promise < void > {
358367// Clear from the REST client. An empty url will indicate to other parts of
359368// the code that we are logged out.
360369this . restClient . setHost ( "" ) ;
@@ -373,7 +382,7 @@ export class Commands{
373382. showInformationMessage ( "You've been logged out of Coder!" , "Login" )
374383. then ( ( action ) => {
375384if ( action === "Login" ) {
376- vscode . commands . executeCommand ( "coder. login" ) ;
385+ this . login ( ) ;
377386}
378387} ) ;
379388
0 commit comments