@@ -305,9 +305,11 @@ export class Commands {
305305}
306306
307307if ( choice === "oauth" ) {
308- return this . loginWithOAuth ( url , client ) ;
308+ return this . loginWithOAuth ( client ) ;
309309} else if ( choice === "legacy" ) {
310- return this . loginWithToken ( url , token , client ) ;
310+ const initialToken =
311+ token || ( await this . secretsManager . getSessionToken ( ) ) ;
312+ return this . loginWithToken ( client , initialToken ) ;
311313}
312314
313315// User aborted.
@@ -350,10 +352,13 @@ export class Commands {
350352}
351353
352354private async loginWithToken (
353- url :string ,
354- token :string | undefined ,
355355client :CoderApi ,
356+ initialToken :string | undefined ,
356357) :Promise < { user :User ; token :string } | null > {
358+ const url = client . getAxiosInstance ( ) . defaults . baseURL ;
359+ if ( ! url ) {
360+ throw new Error ( "No base URL set on REST client" ) ;
361+ }
357362// This prompt is for convenience; do not error if they close it since
358363// they may already have a token or already have the page opened.
359364await vscode . env . openExternal ( vscode . Uri . parse ( `${ url } /cli-auth` ) ) ;
@@ -366,7 +371,7 @@ export class Commands {
366371title :"Coder API Key" ,
367372password :true ,
368373placeHolder :"Paste your API key." ,
369- value :token || ( await this . secretsManager . getSessionToken ( ) ) ,
374+ value :initialToken ,
370375ignoreFocusOut :true ,
371376validateInput :async ( value ) => {
372377if ( ! value ) {
@@ -410,29 +415,17 @@ export class Commands {
410415 * Returns the access token and authenticated user, or null if failed/cancelled.
411416 */
412417private async loginWithOAuth (
413- url :string ,
414418client :CoderApi ,
415419) :Promise < { user :User ; token :string } | null > {
416420try {
417421this . logger . info ( "Starting OAuth authentication" ) ;
418422
419- // Start OAuth authorization flow
420- // TODO just pass the client here and do all the neccessary steps (If we are already logged in we'd have the right token and the OAuth client registration saved).
421- const { code, verifier} =
422- await this . oauthSessionManager . startAuthorization ( url ) ;
423-
424- // Exchange authorization code for tokens
425- const tokenResponse = await this . oauthSessionManager . exchangeToken (
426- code ,
427- verifier ,
428- ) ;
423+ const tokenResponse = await this . oauthSessionManager . login ( client ) ;
429424
430425// Validate token by fetching user
431426client . setSessionToken ( tokenResponse . access_token ) ;
432427const user = await client . getAuthenticatedUser ( ) ;
433428
434- this . logger . info ( "OAuth authentication successful" ) ;
435-
436429return {
437430token :tokenResponse . access_token ,
438431user,
@@ -491,9 +484,9 @@ export class Commands {
491484this . logger . info ( "Logging out" ) ;
492485
493486// Check if using OAuth
494- // TODO maybe just add this check inside oauthSessionManager
495- const hasOAuthTokens = await this . secretsManager . getOAuthTokens ( ) ;
496- if ( hasOAuthTokens ) {
487+ const isOAuthLoggedIn =
488+ await this . oauthSessionManager . isLoggedInWithOAuth ( ) ;
489+ if ( isOAuthLoggedIn ) {
497490this . logger . info ( "Logging out via OAuth" ) ;
498491try {
499492await this . oauthSessionManager . logout ( ) ;