@@ -11,6 +11,7 @@ import { getClerkBaseUrl, getRooCodeApiUrl } from "./Config"
1111import { RefreshTimer } from "./RefreshTimer"
1212
1313export interface AuthServiceEvents {
14+ "inactive-session" : [ data : { previousState : AuthState } ]
1415 "active-session" : [ data : { previousState : AuthState } ]
1516 "logged-out" : [ data : { previousState : AuthState } ]
1617 "user-info" : [ data : { userInfo : CloudUserInfo } ]
@@ -32,15 +33,17 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
3233 private context : vscode . ExtensionContext
3334 private timer : RefreshTimer
3435 private state : AuthState = "initializing"
36+ private log : ( ...args : unknown [ ] ) => void
3537
3638 private credentials : AuthCredentials | null = null
3739 private sessionToken : string | null = null
3840 private userInfo : CloudUserInfo | null = null
3941
40- constructor ( context : vscode . ExtensionContext ) {
42+ constructor ( context : vscode . ExtensionContext , log ?: ( ... args : unknown [ ] ) => void ) {
4143 super ( )
4244
4345 this . context = context
46+ this . log = log || console . log
4447
4548 this . timer = new RefreshTimer ( {
4649 callback : async ( ) => {
@@ -71,7 +74,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
7174 }
7275 }
7376 } catch ( error ) {
74- console . error ( "[auth] Error handling credentials change:" , error )
77+ this . log ( "[auth] Error handling credentials change:" , error )
7578 }
7679 }
7780
@@ -87,19 +90,23 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
8790
8891 this . emit ( "logged-out" , { previousState } )
8992
90- console . log ( "[auth] Transitioned to logged-out state" )
93+ this . log ( "[auth] Transitioned to logged-out state" )
9194 }
9295
9396 private transitionToInactiveSession ( credentials : AuthCredentials ) : void {
9497 this . credentials = credentials
98+
99+ const previousState = this . state
95100 this . state = "inactive-session"
96101
97102 this . sessionToken = null
98103 this . userInfo = null
99104
105+ this . emit ( "inactive-session" , { previousState } )
106+
100107 this . timer . start ( )
101108
102- console . log ( "[auth] Transitioned to inactive-session state" )
109+ this . log ( "[auth] Transitioned to inactive-session state" )
103110 }
104111
105112 /**
@@ -110,7 +117,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
110117 */
111118 public async initialize ( ) : Promise < void > {
112119 if ( this . state !== "initializing" ) {
113- console . log ( "[auth] initialize() called after already initialized" )
120+ this . log ( "[auth] initialize() called after already initialized" )
114121 return
115122 }
116123
@@ -138,9 +145,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
138145 return authCredentialsSchema . parse ( parsedJson )
139146 } catch ( error ) {
140147 if ( error instanceof z . ZodError ) {
141- console . error ( "[auth] Invalid credentials format:" , error . errors )
148+ this . log ( "[auth] Invalid credentials format:" , error . errors )
142149 } else {
143- console . error ( "[auth] Failed to parse stored credentials:" , error )
150+ this . log ( "[auth] Failed to parse stored credentials:" , error )
144151 }
145152 return null
146153 }
@@ -171,7 +178,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
171178 const url = `${ getRooCodeApiUrl ( ) } /extension/sign-in?${ params . toString ( ) } `
172179 await vscode . env . openExternal ( vscode . Uri . parse ( url ) )
173180 } catch ( error ) {
174- console . error ( `[auth] Error initiating Roo Code Cloud auth: ${ error } ` )
181+ this . log ( `[auth] Error initiating Roo Code Cloud auth: ${ error } ` )
175182 throw new Error ( `Failed to initiate Roo Code Cloud authentication: ${ error } ` )
176183 }
177184 }
@@ -196,7 +203,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
196203 const storedState = this . context . globalState . get ( AUTH_STATE_KEY )
197204
198205 if ( state !== storedState ) {
199- console . log ( "[auth] State mismatch in callback" )
206+ this . log ( "[auth] State mismatch in callback" )
200207 throw new Error ( "Invalid state parameter. Authentication request may have been tampered with." )
201208 }
202209
@@ -205,9 +212,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
205212 await this . storeCredentials ( credentials )
206213
207214 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
208- console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
215+ this . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
209216 } catch ( error ) {
210- console . log ( `[auth] Error handling Roo Code Cloud callback: ${ error } ` )
217+ this . log ( `[auth] Error handling Roo Code Cloud callback: ${ error } ` )
211218 const previousState = this . state
212219 this . state = "logged-out"
213220 this . emit ( "logged-out" , { previousState } )
@@ -232,14 +239,14 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
232239 try {
233240 await this . clerkLogout ( oldCredentials )
234241 } catch ( error ) {
235- console . error ( "[auth] Error calling clerkLogout:" , error )
242+ this . log ( "[auth] Error calling clerkLogout:" , error )
236243 }
237244 }
238245
239246 vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
240- console . log ( "[auth] Logged out from Roo Code Cloud" )
247+ this . log ( "[auth] Logged out from Roo Code Cloud" )
241248 } catch ( error ) {
242- console . log ( `[auth] Error logging out from Roo Code Cloud: ${ error } ` )
249+ this . log ( `[auth] Error logging out from Roo Code Cloud: ${ error } ` )
243250 throw new Error ( `Failed to log out from Roo Code Cloud: ${ error } ` )
244251 }
245252 }
@@ -276,19 +283,24 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
276283 */
277284 private async refreshSession ( ) : Promise < void > {
278285 if ( ! this . credentials ) {
279- console . log ( "[auth] Cannot refresh session: missing credentials" )
286+ this . log ( "[auth] Cannot refresh session: missing credentials" )
280287 this . state = "inactive-session"
281288 return
282289 }
283290
284- const previousState = this . state
285- this . sessionToken = await this . clerkCreateSessionToken ( )
286- this . state = "active-session"
291+ try {
292+ const previousState = this . state
293+ this . sessionToken = await this . clerkCreateSessionToken ( )
294+ this . state = "active-session"
287295
288- if ( previousState !== "active-session" ) {
289- console . log ( "[auth] Transitioned to active-session state" )
290- this . emit ( "active-session" , { previousState } )
291- this . fetchUserInfo ( )
296+ if ( previousState !== "active-session" ) {
297+ this . log ( "[auth] Transitioned to active-session state" )
298+ this . emit ( "active-session" , { previousState } )
299+ this . fetchUserInfo ( )
300+ }
301+ } catch ( error ) {
302+ this . log ( "[auth] Failed to refresh session" , error )
303+ throw error
292304 }
293305 }
294306
@@ -436,12 +448,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
436448 return this . _instance
437449 }
438450
439- static async createInstance ( context : vscode . ExtensionContext ) {
451+ static async createInstance ( context : vscode . ExtensionContext , log ?: ( ... args : unknown [ ] ) => void ) {
440452 if ( this . _instance ) {
441453 throw new Error ( "AuthService instance already created" )
442454 }
443455
444- this . _instance = new AuthService ( context )
456+ this . _instance = new AuthService ( context , log )
445457 await this . _instance . initialize ( )
446458 return this . _instance
447459 }
0 commit comments