@@ -21,6 +21,7 @@ export interface AuthServiceEvents {
2121const authCredentialsSchema = z . object ( {
2222 clientToken : z . string ( ) . min ( 1 , "Client token cannot be empty" ) ,
2323 sessionId : z . string ( ) . min ( 1 , "Session ID cannot be empty" ) ,
24+ organizationId : z . string ( ) . nullable ( ) . optional ( ) ,
2425} )
2526
2627type AuthCredentials = z . infer < typeof authCredentialsSchema >
@@ -220,7 +221,16 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
220221
221222 try {
222223 const parsedJson = JSON . parse ( credentialsJson )
223- return authCredentialsSchema . parse ( parsedJson )
224+ const credentials = authCredentialsSchema . parse ( parsedJson )
225+
226+ // Migration: If no organizationId but we have userInfo, add it
227+ if ( credentials . organizationId === undefined && this . userInfo ?. organizationId ) {
228+ credentials . organizationId = this . userInfo . organizationId
229+ await this . storeCredentials ( credentials )
230+ this . log ( "[auth] Migrated credentials with organizationId" )
231+ }
232+
233+ return credentials
224234 } catch ( error ) {
225235 if ( error instanceof z . ZodError ) {
226236 this . log ( "[auth] Invalid credentials format:" , error . errors )
@@ -269,8 +279,13 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
269279 *
270280 * @param code The authorization code from the callback
271281 * @param state The state parameter from the callback
282+ * @param organizationId The organization ID from the callback (null for personal accounts)
272283 */
273- public async handleCallback ( code : string | null , state : string | null ) : Promise < void > {
284+ public async handleCallback (
285+ code : string | null ,
286+ state : string | null ,
287+ organizationId ?: string | null ,
288+ ) : Promise < void > {
274289 if ( ! code || ! state ) {
275290 vscode . window . showInformationMessage ( "Invalid Roo Code Cloud sign in url" )
276291 return
@@ -287,6 +302,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
287302
288303 const credentials = await this . clerkSignIn ( code )
289304
305+ // Set organizationId (null for personal accounts)
306+ credentials . organizationId = organizationId || null
307+
290308 await this . storeCredentials ( credentials )
291309
292310 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
@@ -417,6 +435,15 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
417435 return this . userInfo
418436 }
419437
438+ /**
439+ * Get the stored organization ID from credentials
440+ *
441+ * @returns The stored organization ID, null for personal accounts or if no credentials exist
442+ */
443+ public getStoredOrganizationId ( ) : string | null {
444+ return this . credentials ?. organizationId || null
445+ }
446+
420447 private async clerkSignIn ( ticket : string ) : Promise < AuthCredentials > {
421448 const formData = new URLSearchParams ( )
422449 formData . append ( "strategy" , "ticket" )
@@ -454,6 +481,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
454481 const formData = new URLSearchParams ( )
455482 formData . append ( "_is_native" , "1" )
456483
484+ // Only add organization_id if not null (personal accounts)
485+ const organizationId = this . getStoredOrganizationId ( )
486+ if ( organizationId !== null ) {
487+ formData . append ( "organization_id" , organizationId )
488+ }
489+
457490 const response = await fetch ( `${ getClerkBaseUrl ( ) } /v1/client/sessions/${ this . credentials ! . sessionId } /tokens` , {
458491 method : "POST" ,
459492 headers : {
0 commit comments