@@ -6,6 +6,7 @@ import * as vscode from "vscode"
66
77import type { CloudUserInfo } from "@roo-code/types"
88
9+ import { CloudServiceCallbacks } from "./types"
910import { getClerkBaseUrl , getRooCodeApiUrl } from "./Config"
1011import { RefreshTimer } from "./RefreshTimer"
1112
@@ -22,27 +23,28 @@ type AuthState = "initializing" | "logged-out" | "active-session" | "inactive-se
2223
2324export class AuthService extends EventEmitter < AuthServiceEvents > {
2425 private context : vscode . ExtensionContext
26+ private userChanged : CloudServiceCallbacks [ "userChanged" ]
27+ private timer : RefreshTimer
2528 private state : AuthState = "initializing"
2629
2730 private clientToken : string | null = null
2831 private sessionToken : string | null = null
2932 private sessionId : string | null = null
3033
31- private timer : RefreshTimer
32-
33- constructor ( context : vscode . ExtensionContext , onUserInfo : ( userInfo : CloudUserInfo | undefined ) => void ) {
34+ constructor ( context : vscode . ExtensionContext , userChanged : CloudServiceCallbacks [ "userChanged" ] ) {
3435 super ( )
3536
3637 this . context = context
38+ this . userChanged = userChanged
3739
3840 this . timer = new RefreshTimer ( {
3941 callback : async ( ) => {
40- await this . refreshSession ( onUserInfo )
42+ await this . refreshSession ( )
4143 return true
4244 } ,
43- successInterval : 50000 ,
44- initialBackoffMs : 1000 ,
45- maxBackoffMs : 300000 ,
45+ successInterval : 50_000 ,
46+ initialBackoffMs : 1_000 ,
47+ maxBackoffMs : 300_000 ,
4648 } )
4749 }
4850
@@ -62,7 +64,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
6264 this . clientToken = ( await this . context . secrets . get ( CLIENT_TOKEN_KEY ) ) || null
6365 this . sessionId = this . context . globalState . get < string > ( SESSION_ID_KEY ) || null
6466
65- // Determine initial state
67+ // Determine initial state.
6668 if ( ! this . clientToken || ! this . sessionId ) {
6769 // TODO: it may be possible to get a new session with the client,
6870 // but the obvious Clerk endpoints don't support that.
@@ -109,18 +111,14 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
109111 * @param code The authorization code from the callback
110112 * @param state The state parameter from the callback
111113 */
112- public async handleCallback (
113- code : string | null ,
114- state : string | null ,
115- onUserInfo ?: ( userInfo : CloudUserInfo | undefined ) => void ,
116- ) : Promise < void > {
114+ public async handleCallback ( code : string | null , state : string | null ) : Promise < void > {
117115 if ( ! code || ! state ) {
118116 vscode . window . showInformationMessage ( "Invalid Roo Code Cloud sign in url" )
119117 return
120118 }
121119
122120 try {
123- // 1. Validate state parameter to prevent CSRF attacks
121+ // Validate state parameter to prevent CSRF attacks.
124122 const storedState = this . context . globalState . get ( AUTH_STATE_KEY )
125123
126124 if ( state !== storedState ) {
@@ -142,11 +140,10 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
142140 this . emit ( "active-session" , { previousState } )
143141 this . timer . start ( )
144142
145- if ( onUserInfo ) {
146- this . getUserInfo ( ) . then ( onUserInfo )
143+ if ( this . userChanged ) {
144+ this . getUserInfo ( ) . then ( this . userChanged )
147145 }
148146
149- // Show success message
150147 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
151148 console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
152149 } catch ( error ) {
@@ -163,7 +160,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
163160 *
164161 * This method removes all stored tokens and stops the refresh timer.
165162 */
166- public async logout ( onUserInfo ?: ( userInfo : CloudUserInfo | undefined ) => void ) : Promise < void > {
163+ public async logout ( ) : Promise < void > {
167164 try {
168165 this . timer . stop ( )
169166
@@ -185,8 +182,8 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
185182 await this . clerkLogout ( oldClientToken , oldSessionId )
186183 }
187184
188- if ( onUserInfo ) {
189- this . getUserInfo ( ) . then ( onUserInfo )
185+ if ( this . userChanged ) {
186+ this . getUserInfo ( ) . then ( this . userChanged )
190187 }
191188
192189 vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
@@ -227,7 +224,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
227224 *
228225 * This method refreshes the session token using the client token.
229226 */
230- private async refreshSession ( onUserInfo : ( userInfo : CloudUserInfo | undefined ) => void ) : Promise < void > {
227+ private async refreshSession ( ) {
231228 if ( ! this . sessionId || ! this . clientToken ) {
232229 console . log ( "[auth] Cannot refresh session: missing session ID or token" )
233230 this . state = "inactive-session"
@@ -240,7 +237,10 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
240237
241238 if ( previousState !== "active-session" ) {
242239 this . emit ( "active-session" , { previousState } )
243- this . getUserInfo ( ) . then ( onUserInfo )
240+
241+ if ( this . userChanged ) {
242+ this . getUserInfo ( ) . then ( this . userChanged )
243+ }
244244 }
245245 }
246246
@@ -383,15 +383,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
383383 return this . _instance
384384 }
385385
386- static async createInstance (
387- context : vscode . ExtensionContext ,
388- onUserInfo : ( userInfo : CloudUserInfo | undefined ) => void ,
389- ) {
386+ static async createInstance ( context : vscode . ExtensionContext , userChanged : CloudServiceCallbacks [ "userChanged" ] ) {
390387 if ( this . _instance ) {
391388 throw new Error ( "AuthService instance already created" )
392389 }
393390
394- this . _instance = new AuthService ( context , onUserInfo )
391+ this . _instance = new AuthService ( context , userChanged )
395392 await this . _instance . initialize ( )
396393 return this . _instance
397394 }
0 commit comments