@@ -53,6 +53,55 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
5353 } )
5454 }
5555
56+ private async handleCredentialsChange ( ) : Promise < void > {
57+ try {
58+ const credentials = await this . loadCredentials ( )
59+
60+ if ( credentials ) {
61+ if (
62+ this . credentials === null ||
63+ this . credentials . clientToken !== credentials . clientToken ||
64+ this . credentials . sessionId !== credentials . sessionId
65+ ) {
66+ this . transitionToInactiveSession ( credentials )
67+ }
68+ } else {
69+ if ( this . state !== "logged-out" ) {
70+ this . transitionToLoggedOut ( )
71+ }
72+ }
73+ } catch ( error ) {
74+ console . error ( "[auth] Error handling credentials change:" , error )
75+ }
76+ }
77+
78+ private transitionToLoggedOut ( ) : void {
79+ this . timer . stop ( )
80+
81+ const previousState = this . state
82+
83+ this . credentials = null
84+ this . sessionToken = null
85+ this . userInfo = null
86+ this . state = "logged-out"
87+
88+ this . emit ( "logged-out" , { previousState } )
89+
90+ console . log ( "[auth] Transitioned to logged-out state" )
91+ }
92+
93+ private transitionToInactiveSession ( credentials : AuthCredentials ) : void {
94+ this . credentials = credentials
95+ this . state = "inactive-session"
96+
97+ this . sessionToken = null
98+ this . userInfo = null
99+
100+ this . timer . start ( )
101+
102+ console . log ( "[auth] Transitioned to inactive-session state" )
103+ }
104+
56105 /**
57106 * Initialize the auth state
58107 *
@@ -65,24 +114,15 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
65114 return
66115 }
67116
68- try {
69- const credentials = await this . loadCredentials ( )
117+ this . handleCredentialsChange ( )
70118
71- if ( credentials ) {
72- this . credentials = credentials
73- this . state = "inactive-session"
74- this . timer . start ( )
75- } else {
76- const previousState = this . state
77- this . state = "logged-out"
78- this . emit ( "logged-out" , { previousState } )
79- }
80-
81- console . log ( `[auth] Initialized with state: ${ this . state } ` )
82- } catch ( error ) {
83- console . error ( `[auth] Error initializing AuthService: ${ error } ` )
84- this . state = "logged-out"
85- }
119+ this . context . subscriptions . push (
120+ this . context . secrets . onDidChange ( ( e ) => {
121+ if ( e . key === AUTH_CREDENTIALS_KEY ) {
122+ this . handleCredentialsChange ( )
123+ }
124+ } ) ,
125+ )
86126 }
87127
88128 private async storeCredentials ( credentials : AuthCredentials ) : Promise < void > {
@@ -95,7 +135,6 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
95135
96136 try {
97137 const parsedJson = JSON . parse ( credentialsJson )
98- // Validate using Zod schema
99138 return authCredentialsSchema . parse ( parsedJson )
100139 } catch ( error ) {
101140 if ( error instanceof z . ZodError ) {
@@ -161,20 +200,10 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
161200 throw new Error ( "Invalid state parameter. Authentication request may have been tampered with." )
162201 }
163202
164- const { credentials, sessionToken } = await this . clerkSignIn ( code )
203+ const { credentials } = await this . clerkSignIn ( code )
165204
166205 await this . storeCredentials ( credentials )
167206
168- this . credentials = credentials
169- this . sessionToken = sessionToken
170-
171- const previousState = this . state
172- this . state = "active-session"
173- this . emit ( "active-session" , { previousState } )
174- this . timer . start ( )
175-
176- this . fetchUserInfo ( )
177-
178207 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
179208 console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
180209 } catch ( error ) {
@@ -192,27 +221,21 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
192221 * This method removes all stored tokens and stops the refresh timer.
193222 */
194223 public async logout ( ) : Promise < void > {
195- try {
196- this . timer . stop ( )
224+ const oldCredentials = this . credentials
197225
226+ try {
227+ // Clear credentials from storage - onDidChange will handle state transitions
198228 await this . clearCredentials ( )
199229 await this . context . globalState . update ( AUTH_STATE_KEY , undefined )
200230
201- const oldCredentials = this . credentials
202-
203- this . credentials = null
204- this . sessionToken = null
205- this . userInfo = null
206- const previousState = this . state
207- this . state = "logged-out"
208- this . emit ( "logged-out" , { previousState } )
209-
210231 if ( oldCredentials ) {
211- await this . clerkLogout ( oldCredentials )
232+ try {
233+ await this . clerkLogout ( oldCredentials )
234+ } catch ( error ) {
235+ console . error ( "[auth] Error calling clerkLogout:" , error )
236+ }
212237 }
213238
214- this . fetchUserInfo ( )
215-
216239 vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
217240 console . log ( "[auth] Logged out from Roo Code Cloud" )
218241 } catch ( error ) {
@@ -263,6 +286,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
263286 this . state = "active-session"
264287
265288 if ( previousState !== "active-session" ) {
289+ console . log ( "[auth] Transitioned to active-session state" )
266290 this . emit ( "active-session" , { previousState } )
267291 this . fetchUserInfo ( )
268292 }
0 commit comments