@@ -53,6 +53,55 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
53
53
} )
54
54
}
55
55
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
+
56
105
/**
57
106
* Initialize the auth state
58
107
*
@@ -65,24 +114,15 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
65
114
return
66
115
}
67
116
68
- try {
69
- const credentials = await this . loadCredentials ( )
117
+ this . handleCredentialsChange ( )
70
118
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
+ )
86
126
}
87
127
88
128
private async storeCredentials ( credentials : AuthCredentials ) : Promise < void > {
@@ -95,7 +135,6 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
95
135
96
136
try {
97
137
const parsedJson = JSON . parse ( credentialsJson )
98
- // Validate using Zod schema
99
138
return authCredentialsSchema . parse ( parsedJson )
100
139
} catch ( error ) {
101
140
if ( error instanceof z . ZodError ) {
@@ -161,20 +200,10 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
161
200
throw new Error ( "Invalid state parameter. Authentication request may have been tampered with." )
162
201
}
163
202
164
- const { credentials, sessionToken } = await this . clerkSignIn ( code )
203
+ const { credentials } = await this . clerkSignIn ( code )
165
204
166
205
await this . storeCredentials ( credentials )
167
206
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
-
178
207
vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
179
208
console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
180
209
} catch ( error ) {
@@ -192,27 +221,21 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
192
221
* This method removes all stored tokens and stops the refresh timer.
193
222
*/
194
223
public async logout ( ) : Promise < void > {
195
- try {
196
- this . timer . stop ( )
224
+ const oldCredentials = this . credentials
197
225
226
+ try {
227
+ // Clear credentials from storage - onDidChange will handle state transitions
198
228
await this . clearCredentials ( )
199
229
await this . context . globalState . update ( AUTH_STATE_KEY , undefined )
200
230
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
-
210
231
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
+ }
212
237
}
213
238
214
- this . fetchUserInfo ( )
215
-
216
239
vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
217
240
console . log ( "[auth] Logged out from Roo Code Cloud" )
218
241
} catch ( error ) {
@@ -263,6 +286,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
263
286
this . state = "active-session"
264
287
265
288
if ( previousState !== "active-session" ) {
289
+ console . log ( "[auth] Transitioned to active-session state" )
266
290
this . emit ( "active-session" , { previousState } )
267
291
this . fetchUserInfo ( )
268
292
}
0 commit comments