@blackcode_sa/metaestetics-api 1.12.10 → 1.12.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -6191,6 +6191,17 @@ declare class AuthService extends BaseService {
6191
6191
  * @returns The signed-in or newly created user.
6192
6192
  */
6193
6193
  signInWithGoogleIdToken(idToken: string, initialRole?: UserRole): Promise<User>;
6194
+ /**
6195
+ * Signs in a user with a Google authorization code from a mobile client.
6196
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
6197
+ * with the platform-specific client ID (no client secret needed for native apps).
6198
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
6199
+ * @param redirectUri - The redirect URI used in the OAuth flow.
6200
+ * @param platform - The platform (ios/android) to determine which client ID to use.
6201
+ * @param initialRole - The role to assign to the user if they are being created.
6202
+ * @returns The signed-in or newly created user.
6203
+ */
6204
+ signInWithGoogleAuthCode(authorizationCode: string, redirectUri: string, platform: 'ios' | 'android', initialRole?: UserRole): Promise<User>;
6194
6205
  /**
6195
6206
  * Links a Google account to the currently signed-in user using an ID token.
6196
6207
  * This is used to upgrade an anonymous user or to allow an existing user
@@ -6199,6 +6210,16 @@ declare class AuthService extends BaseService {
6199
6210
  * @returns The updated user profile.
6200
6211
  */
6201
6212
  linkGoogleAccount(idToken: string): Promise<User>;
6213
+ /**
6214
+ * Links a Google account to the currently signed-in user using an authorization code.
6215
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
6216
+ * with the platform-specific client ID, then links the Google account to the current user.
6217
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
6218
+ * @param redirectUri - The redirect URI used in the OAuth flow.
6219
+ * @param platform - The platform (ios/android) to determine which client ID to use.
6220
+ * @returns The updated user profile.
6221
+ */
6222
+ linkGoogleAccountWithAuthCode(authorizationCode: string, redirectUri: string, platform: 'ios' | 'android'): Promise<User>;
6202
6223
  }
6203
6224
 
6204
6225
  /**
package/dist/index.d.ts CHANGED
@@ -6191,6 +6191,17 @@ declare class AuthService extends BaseService {
6191
6191
  * @returns The signed-in or newly created user.
6192
6192
  */
6193
6193
  signInWithGoogleIdToken(idToken: string, initialRole?: UserRole): Promise<User>;
6194
+ /**
6195
+ * Signs in a user with a Google authorization code from a mobile client.
6196
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
6197
+ * with the platform-specific client ID (no client secret needed for native apps).
6198
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
6199
+ * @param redirectUri - The redirect URI used in the OAuth flow.
6200
+ * @param platform - The platform (ios/android) to determine which client ID to use.
6201
+ * @param initialRole - The role to assign to the user if they are being created.
6202
+ * @returns The signed-in or newly created user.
6203
+ */
6204
+ signInWithGoogleAuthCode(authorizationCode: string, redirectUri: string, platform: 'ios' | 'android', initialRole?: UserRole): Promise<User>;
6194
6205
  /**
6195
6206
  * Links a Google account to the currently signed-in user using an ID token.
6196
6207
  * This is used to upgrade an anonymous user or to allow an existing user
@@ -6199,6 +6210,16 @@ declare class AuthService extends BaseService {
6199
6210
  * @returns The updated user profile.
6200
6211
  */
6201
6212
  linkGoogleAccount(idToken: string): Promise<User>;
6213
+ /**
6214
+ * Links a Google account to the currently signed-in user using an authorization code.
6215
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
6216
+ * with the platform-specific client ID, then links the Google account to the current user.
6217
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
6218
+ * @param redirectUri - The redirect URI used in the OAuth flow.
6219
+ * @param platform - The platform (ios/android) to determine which client ID to use.
6220
+ * @returns The updated user profile.
6221
+ */
6222
+ linkGoogleAccountWithAuthCode(authorizationCode: string, redirectUri: string, platform: 'ios' | 'android'): Promise<User>;
6202
6223
  }
6203
6224
 
6204
6225
  /**
package/dist/index.js CHANGED
@@ -10395,6 +10395,72 @@ var AuthService = class extends BaseService {
10395
10395
  throw handleFirebaseError(error);
10396
10396
  }
10397
10397
  }
10398
+ /**
10399
+ * Signs in a user with a Google authorization code from a mobile client.
10400
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
10401
+ * with the platform-specific client ID (no client secret needed for native apps).
10402
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
10403
+ * @param redirectUri - The redirect URI used in the OAuth flow.
10404
+ * @param platform - The platform (ios/android) to determine which client ID to use.
10405
+ * @param initialRole - The role to assign to the user if they are being created.
10406
+ * @returns The signed-in or newly created user.
10407
+ */
10408
+ async signInWithGoogleAuthCode(authorizationCode, redirectUri, platform, initialRole = "patient" /* PATIENT */) {
10409
+ try {
10410
+ console.log("[AUTH] Signing in with Google authorization code (native flow)");
10411
+ console.log("[AUTH] Platform:", platform);
10412
+ console.log("[AUTH] Redirect URI:", redirectUri);
10413
+ console.log("[AUTH] Code length:", authorizationCode.length);
10414
+ const clientId = platform === "ios" ? process.env.GOOGLE_IOS_CLIENT_ID : process.env.GOOGLE_ANDROID_CLIENT_ID;
10415
+ if (!clientId) {
10416
+ throw new Error(
10417
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`
10418
+ );
10419
+ }
10420
+ console.log("[AUTH] Using client ID:", `${clientId.substring(0, 20)}...`);
10421
+ const tokenEndpoint = "https://oauth2.googleapis.com/token";
10422
+ const params = new URLSearchParams({
10423
+ client_id: clientId,
10424
+ code: authorizationCode,
10425
+ grant_type: "authorization_code",
10426
+ redirect_uri: redirectUri
10427
+ // For native apps, we don't include client_secret
10428
+ });
10429
+ console.log("[AUTH] Making request to Google token endpoint...");
10430
+ const response = await fetch(tokenEndpoint, {
10431
+ method: "POST",
10432
+ headers: {
10433
+ "Content-Type": "application/x-www-form-urlencoded"
10434
+ },
10435
+ body: params.toString()
10436
+ });
10437
+ if (!response.ok) {
10438
+ const errorText = await response.text();
10439
+ console.error("[AUTH] Google token exchange failed:", {
10440
+ status: response.status,
10441
+ statusText: response.statusText,
10442
+ error: errorText
10443
+ });
10444
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
10445
+ }
10446
+ const tokenData = await response.json();
10447
+ console.log("[AUTH] Token exchange response received:", {
10448
+ hasIdToken: !!tokenData.id_token,
10449
+ hasAccessToken: !!tokenData.access_token,
10450
+ hasRefreshToken: !!tokenData.refresh_token
10451
+ });
10452
+ if (!tokenData.id_token) {
10453
+ console.error("[AUTH] No ID token in response:", tokenData);
10454
+ throw new Error("No ID token received from Google token exchange");
10455
+ }
10456
+ console.log("[AUTH] Successfully obtained ID token from Google");
10457
+ console.log("[AUTH] ID token length:", tokenData.id_token.length);
10458
+ return await this.signInWithGoogleIdToken(tokenData.id_token, initialRole);
10459
+ } catch (error) {
10460
+ console.error("[AUTH] Error in signInWithGoogleAuthCode:", error);
10461
+ throw handleFirebaseError(error);
10462
+ }
10463
+ }
10398
10464
  /**
10399
10465
  * Links a Google account to the currently signed-in user using an ID token.
10400
10466
  * This is used to upgrade an anonymous user or to allow an existing user
@@ -10428,6 +10494,70 @@ var AuthService = class extends BaseService {
10428
10494
  throw handleFirebaseError(error);
10429
10495
  }
10430
10496
  }
10497
+ /**
10498
+ * Links a Google account to the currently signed-in user using an authorization code.
10499
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
10500
+ * with the platform-specific client ID, then links the Google account to the current user.
10501
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
10502
+ * @param redirectUri - The redirect URI used in the OAuth flow.
10503
+ * @param platform - The platform (ios/android) to determine which client ID to use.
10504
+ * @returns The updated user profile.
10505
+ */
10506
+ async linkGoogleAccountWithAuthCode(authorizationCode, redirectUri, platform) {
10507
+ try {
10508
+ console.log("[AUTH] Linking Google account with authorization code (native flow)");
10509
+ console.log("[AUTH] Platform:", platform);
10510
+ console.log("[AUTH] Redirect URI:", redirectUri);
10511
+ console.log("[AUTH] Code length:", authorizationCode.length);
10512
+ const clientId = platform === "ios" ? process.env.GOOGLE_IOS_CLIENT_ID : process.env.GOOGLE_ANDROID_CLIENT_ID;
10513
+ if (!clientId) {
10514
+ throw new Error(
10515
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`
10516
+ );
10517
+ }
10518
+ console.log("[AUTH] Using client ID:", `${clientId.substring(0, 20)}...`);
10519
+ const tokenEndpoint = "https://oauth2.googleapis.com/token";
10520
+ const params = new URLSearchParams({
10521
+ client_id: clientId,
10522
+ code: authorizationCode,
10523
+ grant_type: "authorization_code",
10524
+ redirect_uri: redirectUri
10525
+ });
10526
+ console.log("[AUTH] Making request to Google token endpoint...");
10527
+ const response = await fetch(tokenEndpoint, {
10528
+ method: "POST",
10529
+ headers: {
10530
+ "Content-Type": "application/x-www-form-urlencoded"
10531
+ },
10532
+ body: params.toString()
10533
+ });
10534
+ if (!response.ok) {
10535
+ const errorText = await response.text();
10536
+ console.error("[AUTH] Google token exchange failed:", {
10537
+ status: response.status,
10538
+ statusText: response.statusText,
10539
+ error: errorText
10540
+ });
10541
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
10542
+ }
10543
+ const tokenData = await response.json();
10544
+ console.log("[AUTH] Token exchange response received:", {
10545
+ hasIdToken: !!tokenData.id_token,
10546
+ hasAccessToken: !!tokenData.access_token,
10547
+ hasRefreshToken: !!tokenData.refresh_token
10548
+ });
10549
+ if (!tokenData.id_token) {
10550
+ console.error("[AUTH] No ID token in response:", tokenData);
10551
+ throw new Error("No ID token received from Google token exchange");
10552
+ }
10553
+ console.log("[AUTH] Successfully obtained ID token from Google");
10554
+ console.log("[AUTH] ID token length:", tokenData.id_token.length);
10555
+ return await this.linkGoogleAccount(tokenData.id_token);
10556
+ } catch (error) {
10557
+ console.error("[AUTH] Error in linkGoogleAccountWithAuthCode:", error);
10558
+ throw handleFirebaseError(error);
10559
+ }
10560
+ }
10431
10561
  };
10432
10562
 
10433
10563
  // src/services/calendar/calendar.v2.service.ts
package/dist/index.mjs CHANGED
@@ -10499,6 +10499,72 @@ var AuthService = class extends BaseService {
10499
10499
  throw handleFirebaseError(error);
10500
10500
  }
10501
10501
  }
10502
+ /**
10503
+ * Signs in a user with a Google authorization code from a mobile client.
10504
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
10505
+ * with the platform-specific client ID (no client secret needed for native apps).
10506
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
10507
+ * @param redirectUri - The redirect URI used in the OAuth flow.
10508
+ * @param platform - The platform (ios/android) to determine which client ID to use.
10509
+ * @param initialRole - The role to assign to the user if they are being created.
10510
+ * @returns The signed-in or newly created user.
10511
+ */
10512
+ async signInWithGoogleAuthCode(authorizationCode, redirectUri, platform, initialRole = "patient" /* PATIENT */) {
10513
+ try {
10514
+ console.log("[AUTH] Signing in with Google authorization code (native flow)");
10515
+ console.log("[AUTH] Platform:", platform);
10516
+ console.log("[AUTH] Redirect URI:", redirectUri);
10517
+ console.log("[AUTH] Code length:", authorizationCode.length);
10518
+ const clientId = platform === "ios" ? process.env.GOOGLE_IOS_CLIENT_ID : process.env.GOOGLE_ANDROID_CLIENT_ID;
10519
+ if (!clientId) {
10520
+ throw new Error(
10521
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`
10522
+ );
10523
+ }
10524
+ console.log("[AUTH] Using client ID:", `${clientId.substring(0, 20)}...`);
10525
+ const tokenEndpoint = "https://oauth2.googleapis.com/token";
10526
+ const params = new URLSearchParams({
10527
+ client_id: clientId,
10528
+ code: authorizationCode,
10529
+ grant_type: "authorization_code",
10530
+ redirect_uri: redirectUri
10531
+ // For native apps, we don't include client_secret
10532
+ });
10533
+ console.log("[AUTH] Making request to Google token endpoint...");
10534
+ const response = await fetch(tokenEndpoint, {
10535
+ method: "POST",
10536
+ headers: {
10537
+ "Content-Type": "application/x-www-form-urlencoded"
10538
+ },
10539
+ body: params.toString()
10540
+ });
10541
+ if (!response.ok) {
10542
+ const errorText = await response.text();
10543
+ console.error("[AUTH] Google token exchange failed:", {
10544
+ status: response.status,
10545
+ statusText: response.statusText,
10546
+ error: errorText
10547
+ });
10548
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
10549
+ }
10550
+ const tokenData = await response.json();
10551
+ console.log("[AUTH] Token exchange response received:", {
10552
+ hasIdToken: !!tokenData.id_token,
10553
+ hasAccessToken: !!tokenData.access_token,
10554
+ hasRefreshToken: !!tokenData.refresh_token
10555
+ });
10556
+ if (!tokenData.id_token) {
10557
+ console.error("[AUTH] No ID token in response:", tokenData);
10558
+ throw new Error("No ID token received from Google token exchange");
10559
+ }
10560
+ console.log("[AUTH] Successfully obtained ID token from Google");
10561
+ console.log("[AUTH] ID token length:", tokenData.id_token.length);
10562
+ return await this.signInWithGoogleIdToken(tokenData.id_token, initialRole);
10563
+ } catch (error) {
10564
+ console.error("[AUTH] Error in signInWithGoogleAuthCode:", error);
10565
+ throw handleFirebaseError(error);
10566
+ }
10567
+ }
10502
10568
  /**
10503
10569
  * Links a Google account to the currently signed-in user using an ID token.
10504
10570
  * This is used to upgrade an anonymous user or to allow an existing user
@@ -10532,6 +10598,70 @@ var AuthService = class extends BaseService {
10532
10598
  throw handleFirebaseError(error);
10533
10599
  }
10534
10600
  }
10601
+ /**
10602
+ * Links a Google account to the currently signed-in user using an authorization code.
10603
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
10604
+ * with the platform-specific client ID, then links the Google account to the current user.
10605
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
10606
+ * @param redirectUri - The redirect URI used in the OAuth flow.
10607
+ * @param platform - The platform (ios/android) to determine which client ID to use.
10608
+ * @returns The updated user profile.
10609
+ */
10610
+ async linkGoogleAccountWithAuthCode(authorizationCode, redirectUri, platform) {
10611
+ try {
10612
+ console.log("[AUTH] Linking Google account with authorization code (native flow)");
10613
+ console.log("[AUTH] Platform:", platform);
10614
+ console.log("[AUTH] Redirect URI:", redirectUri);
10615
+ console.log("[AUTH] Code length:", authorizationCode.length);
10616
+ const clientId = platform === "ios" ? process.env.GOOGLE_IOS_CLIENT_ID : process.env.GOOGLE_ANDROID_CLIENT_ID;
10617
+ if (!clientId) {
10618
+ throw new Error(
10619
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`
10620
+ );
10621
+ }
10622
+ console.log("[AUTH] Using client ID:", `${clientId.substring(0, 20)}...`);
10623
+ const tokenEndpoint = "https://oauth2.googleapis.com/token";
10624
+ const params = new URLSearchParams({
10625
+ client_id: clientId,
10626
+ code: authorizationCode,
10627
+ grant_type: "authorization_code",
10628
+ redirect_uri: redirectUri
10629
+ });
10630
+ console.log("[AUTH] Making request to Google token endpoint...");
10631
+ const response = await fetch(tokenEndpoint, {
10632
+ method: "POST",
10633
+ headers: {
10634
+ "Content-Type": "application/x-www-form-urlencoded"
10635
+ },
10636
+ body: params.toString()
10637
+ });
10638
+ if (!response.ok) {
10639
+ const errorText = await response.text();
10640
+ console.error("[AUTH] Google token exchange failed:", {
10641
+ status: response.status,
10642
+ statusText: response.statusText,
10643
+ error: errorText
10644
+ });
10645
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
10646
+ }
10647
+ const tokenData = await response.json();
10648
+ console.log("[AUTH] Token exchange response received:", {
10649
+ hasIdToken: !!tokenData.id_token,
10650
+ hasAccessToken: !!tokenData.access_token,
10651
+ hasRefreshToken: !!tokenData.refresh_token
10652
+ });
10653
+ if (!tokenData.id_token) {
10654
+ console.error("[AUTH] No ID token in response:", tokenData);
10655
+ throw new Error("No ID token received from Google token exchange");
10656
+ }
10657
+ console.log("[AUTH] Successfully obtained ID token from Google");
10658
+ console.log("[AUTH] ID token length:", tokenData.id_token.length);
10659
+ return await this.linkGoogleAccount(tokenData.id_token);
10660
+ } catch (error) {
10661
+ console.error("[AUTH] Error in linkGoogleAccountWithAuthCode:", error);
10662
+ throw handleFirebaseError(error);
10663
+ }
10664
+ }
10535
10665
  };
10536
10666
 
10537
10667
  // src/services/calendar/calendar.v2.service.ts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.12.10",
4
+ "version": "1.12.11",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -903,6 +903,98 @@ export class AuthService extends BaseService {
903
903
  }
904
904
  }
905
905
 
906
+ /**
907
+ * Signs in a user with a Google authorization code from a mobile client.
908
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
909
+ * with the platform-specific client ID (no client secret needed for native apps).
910
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
911
+ * @param redirectUri - The redirect URI used in the OAuth flow.
912
+ * @param platform - The platform (ios/android) to determine which client ID to use.
913
+ * @param initialRole - The role to assign to the user if they are being created.
914
+ * @returns The signed-in or newly created user.
915
+ */
916
+ async signInWithGoogleAuthCode(
917
+ authorizationCode: string,
918
+ redirectUri: string,
919
+ platform: 'ios' | 'android',
920
+ initialRole: UserRole = UserRole.PATIENT,
921
+ ): Promise<User> {
922
+ try {
923
+ console.log('[AUTH] Signing in with Google authorization code (native flow)');
924
+ console.log('[AUTH] Platform:', platform);
925
+ console.log('[AUTH] Redirect URI:', redirectUri);
926
+ console.log('[AUTH] Code length:', authorizationCode.length);
927
+
928
+ // Get the platform-specific client ID from environment variables
929
+ // These should be the same client IDs used in the mobile app
930
+ const clientId =
931
+ platform === 'ios'
932
+ ? process.env.GOOGLE_IOS_CLIENT_ID
933
+ : process.env.GOOGLE_ANDROID_CLIENT_ID;
934
+
935
+ if (!clientId) {
936
+ throw new Error(
937
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`,
938
+ );
939
+ }
940
+
941
+ console.log('[AUTH] Using client ID:', `${clientId.substring(0, 20)}...`);
942
+
943
+ // Exchange authorization code for tokens using Google's OAuth2 API
944
+ // For native apps, we don't need a client secret
945
+ const tokenEndpoint = 'https://oauth2.googleapis.com/token';
946
+
947
+ const params = new URLSearchParams({
948
+ client_id: clientId,
949
+ code: authorizationCode,
950
+ grant_type: 'authorization_code',
951
+ redirect_uri: redirectUri,
952
+ // For native apps, we don't include client_secret
953
+ });
954
+
955
+ console.log('[AUTH] Making request to Google token endpoint...');
956
+
957
+ const response = await fetch(tokenEndpoint, {
958
+ method: 'POST',
959
+ headers: {
960
+ 'Content-Type': 'application/x-www-form-urlencoded',
961
+ },
962
+ body: params.toString(),
963
+ });
964
+
965
+ if (!response.ok) {
966
+ const errorText = await response.text();
967
+ console.error('[AUTH] Google token exchange failed:', {
968
+ status: response.status,
969
+ statusText: response.statusText,
970
+ error: errorText,
971
+ });
972
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
973
+ }
974
+
975
+ const tokenData = await response.json();
976
+ console.log('[AUTH] Token exchange response received:', {
977
+ hasIdToken: !!tokenData.id_token,
978
+ hasAccessToken: !!tokenData.access_token,
979
+ hasRefreshToken: !!tokenData.refresh_token,
980
+ });
981
+
982
+ if (!tokenData.id_token) {
983
+ console.error('[AUTH] No ID token in response:', tokenData);
984
+ throw new Error('No ID token received from Google token exchange');
985
+ }
986
+
987
+ console.log('[AUTH] Successfully obtained ID token from Google');
988
+ console.log('[AUTH] ID token length:', tokenData.id_token.length);
989
+
990
+ // Now sign in with the ID token
991
+ return await this.signInWithGoogleIdToken(tokenData.id_token, initialRole);
992
+ } catch (error) {
993
+ console.error('[AUTH] Error in signInWithGoogleAuthCode:', error);
994
+ throw handleFirebaseError(error);
995
+ }
996
+ }
997
+
906
998
  /**
907
999
  * Links a Google account to the currently signed-in user using an ID token.
908
1000
  * This is used to upgrade an anonymous user or to allow an existing user
@@ -941,4 +1033,91 @@ export class AuthService extends BaseService {
941
1033
  throw handleFirebaseError(error);
942
1034
  }
943
1035
  }
1036
+
1037
+ /**
1038
+ * Links a Google account to the currently signed-in user using an authorization code.
1039
+ * This method directly exchanges the authorization code for tokens using Google's OAuth2 API
1040
+ * with the platform-specific client ID, then links the Google account to the current user.
1041
+ * @param authorizationCode - The Google authorization code obtained from the mobile app.
1042
+ * @param redirectUri - The redirect URI used in the OAuth flow.
1043
+ * @param platform - The platform (ios/android) to determine which client ID to use.
1044
+ * @returns The updated user profile.
1045
+ */
1046
+ async linkGoogleAccountWithAuthCode(
1047
+ authorizationCode: string,
1048
+ redirectUri: string,
1049
+ platform: 'ios' | 'android',
1050
+ ): Promise<User> {
1051
+ try {
1052
+ console.log('[AUTH] Linking Google account with authorization code (native flow)');
1053
+ console.log('[AUTH] Platform:', platform);
1054
+ console.log('[AUTH] Redirect URI:', redirectUri);
1055
+ console.log('[AUTH] Code length:', authorizationCode.length);
1056
+
1057
+ // Get the platform-specific client ID from environment variables
1058
+ const clientId =
1059
+ platform === 'ios'
1060
+ ? process.env.GOOGLE_IOS_CLIENT_ID
1061
+ : process.env.GOOGLE_ANDROID_CLIENT_ID;
1062
+
1063
+ if (!clientId) {
1064
+ throw new Error(
1065
+ `Missing Google ${platform.toUpperCase()} client ID in environment variables`,
1066
+ );
1067
+ }
1068
+
1069
+ console.log('[AUTH] Using client ID:', `${clientId.substring(0, 20)}...`);
1070
+
1071
+ // Exchange authorization code for tokens using Google's OAuth2 API
1072
+ const tokenEndpoint = 'https://oauth2.googleapis.com/token';
1073
+
1074
+ const params = new URLSearchParams({
1075
+ client_id: clientId,
1076
+ code: authorizationCode,
1077
+ grant_type: 'authorization_code',
1078
+ redirect_uri: redirectUri,
1079
+ });
1080
+
1081
+ console.log('[AUTH] Making request to Google token endpoint...');
1082
+
1083
+ const response = await fetch(tokenEndpoint, {
1084
+ method: 'POST',
1085
+ headers: {
1086
+ 'Content-Type': 'application/x-www-form-urlencoded',
1087
+ },
1088
+ body: params.toString(),
1089
+ });
1090
+
1091
+ if (!response.ok) {
1092
+ const errorText = await response.text();
1093
+ console.error('[AUTH] Google token exchange failed:', {
1094
+ status: response.status,
1095
+ statusText: response.statusText,
1096
+ error: errorText,
1097
+ });
1098
+ throw new Error(`Google token exchange failed: ${response.status} - ${errorText}`);
1099
+ }
1100
+
1101
+ const tokenData = await response.json();
1102
+ console.log('[AUTH] Token exchange response received:', {
1103
+ hasIdToken: !!tokenData.id_token,
1104
+ hasAccessToken: !!tokenData.access_token,
1105
+ hasRefreshToken: !!tokenData.refresh_token,
1106
+ });
1107
+
1108
+ if (!tokenData.id_token) {
1109
+ console.error('[AUTH] No ID token in response:', tokenData);
1110
+ throw new Error('No ID token received from Google token exchange');
1111
+ }
1112
+
1113
+ console.log('[AUTH] Successfully obtained ID token from Google');
1114
+ console.log('[AUTH] ID token length:', tokenData.id_token.length);
1115
+
1116
+ // Now link the Google account using the ID token
1117
+ return await this.linkGoogleAccount(tokenData.id_token);
1118
+ } catch (error) {
1119
+ console.error('[AUTH] Error in linkGoogleAccountWithAuthCode:', error);
1120
+ throw handleFirebaseError(error);
1121
+ }
1122
+ }
944
1123
  }