@@ -6,13 +6,13 @@ import * as vscode from "vscode"
66
77import type { CloudUserInfo } from "@roo-code/types"
88
9- import { CloudServiceCallbacks } from "./types"
109import { getClerkBaseUrl , getRooCodeApiUrl } from "./Config"
1110import { RefreshTimer } from "./RefreshTimer"
1211
1312export interface AuthServiceEvents {
1413 "active-session" : [ data : { previousState : AuthState } ]
1514 "logged-out" : [ data : { previousState : AuthState } ]
15+ "user-info" : [ data : { userInfo : CloudUserInfo } ]
1616}
1717
1818const CLIENT_TOKEN_KEY = "clerk-client-token"
@@ -23,19 +23,18 @@ type AuthState = "initializing" | "logged-out" | "active-session" | "inactive-se
2323
2424export class AuthService extends EventEmitter < AuthServiceEvents > {
2525 private context : vscode . ExtensionContext
26- private userChanged : CloudServiceCallbacks [ "userChanged" ]
2726 private timer : RefreshTimer
2827 private state : AuthState = "initializing"
2928
3029 private clientToken : string | null = null
3130 private sessionToken : string | null = null
3231 private sessionId : string | null = null
32+ private userInfo : CloudUserInfo | null = null
3333
34- constructor ( context : vscode . ExtensionContext , userChanged : CloudServiceCallbacks [ "userChanged" ] ) {
34+ constructor ( context : vscode . ExtensionContext ) {
3535 super ( )
3636
3737 this . context = context
38- this . userChanged = userChanged
3938
4039 this . timer = new RefreshTimer ( {
4140 callback : async ( ) => {
@@ -140,9 +139,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
140139 this . emit ( "active-session" , { previousState } )
141140 this . timer . start ( )
142141
143- if ( this . userChanged ) {
144- this . getUserInfo ( ) . then ( this . userChanged )
145- }
142+ this . fetchUserInfo ( )
146143
147144 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
148145 console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
@@ -174,6 +171,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
174171 this . clientToken = null
175172 this . sessionToken = null
176173 this . sessionId = null
174+ this . userInfo = null
177175 const previousState = this . state
178176 this . state = "logged-out"
179177 this . emit ( "logged-out" , { previousState } )
@@ -182,9 +180,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
182180 await this . clerkLogout ( oldClientToken , oldSessionId )
183181 }
184182
185- if ( this . userChanged ) {
186- this . getUserInfo ( ) . then ( this . userChanged )
187- }
183+ this . fetchUserInfo ( )
188184
189185 vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
190186 console . log ( "[auth] Logged out from Roo Code Cloud" )
@@ -224,7 +220,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
224220 *
225221 * This method refreshes the session token using the client token.
226222 */
227- private async refreshSession ( ) {
223+ private async refreshSession ( ) : Promise < void > {
228224 if ( ! this . sessionId || ! this . clientToken ) {
229225 console . log ( "[auth] Cannot refresh session: missing session ID or token" )
230226 this . state = "inactive-session"
@@ -237,24 +233,26 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
237233
238234 if ( previousState !== "active-session" ) {
239235 this . emit ( "active-session" , { previousState } )
236+ this . fetchUserInfo ( )
237+ }
238+ }
240239
241- if ( this . userChanged ) {
242- this . getUserInfo ( ) . then ( this . userChanged )
243- }
240+ private async fetchUserInfo ( ) : Promise < void > {
241+ if ( ! this . clientToken ) {
242+ return
244243 }
244+
245+ this . userInfo = await this . clerkMe ( )
246+ this . emit ( "user-info" , { userInfo : this . userInfo } )
245247 }
246248
247249 /**
248250 * Extract user information from the ID token
249251 *
250252 * @returns User information from ID token claims or null if no ID token available
251253 */
252- public async getUserInfo ( ) : Promise < CloudUserInfo | undefined > {
253- if ( ! this . clientToken ) {
254- return undefined
255- }
256-
257- return await this . clerkMe ( )
254+ public getUserInfo ( ) : CloudUserInfo | null {
255+ return this . userInfo
258256 }
259257
260258 private async clerkSignIn (
@@ -383,12 +381,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
383381 return this . _instance
384382 }
385383
386- static async createInstance ( context : vscode . ExtensionContext , userChanged : CloudServiceCallbacks [ "userChanged" ] ) {
384+ static async createInstance ( context : vscode . ExtensionContext ) {
387385 if ( this . _instance ) {
388386 throw new Error ( "AuthService instance already created" )
389387 }
390388
391- this . _instance = new AuthService ( context , userChanged )
389+ this . _instance = new AuthService ( context )
392390 await this . _instance . initialize ( )
393391 return this . _instance
394392 }
0 commit comments