react-native-nitro-auth 0.5.4 → 0.5.5

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.
Files changed (43) hide show
  1. package/README.md +60 -30
  2. package/android/src/main/cpp/JniOnLoad.cpp +3 -1
  3. package/android/src/main/cpp/PlatformAuth+Android.cpp +11 -11
  4. package/android/src/main/java/com/auth/AuthAdapter.kt +100 -116
  5. package/android/src/main/java/com/auth/NitroAuthModule.kt +8 -1
  6. package/ios/AuthAdapter.swift +62 -28
  7. package/lib/commonjs/index.js +23 -1
  8. package/lib/commonjs/index.js.map +1 -1
  9. package/lib/commonjs/service.js +31 -6
  10. package/lib/commonjs/service.js.map +1 -1
  11. package/lib/commonjs/use-auth.js +11 -22
  12. package/lib/commonjs/use-auth.js.map +1 -1
  13. package/lib/commonjs/utils/auth-error.js +37 -0
  14. package/lib/commonjs/utils/auth-error.js.map +1 -0
  15. package/lib/module/index.js +1 -0
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/service.js +31 -6
  18. package/lib/module/service.js.map +1 -1
  19. package/lib/module/use-auth.js +11 -22
  20. package/lib/module/use-auth.js.map +1 -1
  21. package/lib/module/utils/auth-error.js +30 -0
  22. package/lib/module/utils/auth-error.js.map +1 -0
  23. package/lib/typescript/commonjs/index.d.ts +1 -0
  24. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  25. package/lib/typescript/commonjs/service.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/use-auth.d.ts +2 -1
  27. package/lib/typescript/commonjs/use-auth.d.ts.map +1 -1
  28. package/lib/typescript/commonjs/utils/auth-error.d.ts +16 -0
  29. package/lib/typescript/commonjs/utils/auth-error.d.ts.map +1 -0
  30. package/lib/typescript/module/index.d.ts +1 -0
  31. package/lib/typescript/module/index.d.ts.map +1 -1
  32. package/lib/typescript/module/service.d.ts.map +1 -1
  33. package/lib/typescript/module/use-auth.d.ts +2 -1
  34. package/lib/typescript/module/use-auth.d.ts.map +1 -1
  35. package/lib/typescript/module/utils/auth-error.d.ts +16 -0
  36. package/lib/typescript/module/utils/auth-error.d.ts.map +1 -0
  37. package/nitrogen/generated/android/NitroAuthOnLoad.cpp +22 -17
  38. package/nitrogen/generated/android/NitroAuthOnLoad.hpp +13 -4
  39. package/package.json +7 -7
  40. package/src/index.ts +1 -0
  41. package/src/service.ts +32 -6
  42. package/src/use-auth.ts +21 -86
  43. package/src/utils/auth-error.ts +49 -0
package/README.md CHANGED
@@ -71,11 +71,11 @@ bun add react-native-nitro-auth react-native-nitro-modules
71
71
 
72
72
  ### Requirements
73
73
 
74
- | Dependency | Version |
75
- | ---------------------------- | ----------- |
76
- | `react-native` | `>= 0.75.0` |
77
- | `react-native-nitro-modules` | `>= 0.33.9` |
78
- | `react` | `*` |
74
+ | Dependency | Version |
75
+ | ---------------------------- | ------------ |
76
+ | `react-native` | `>= 0.75.0` |
77
+ | `react-native-nitro-modules` | `>= 0.35.0` |
78
+ | `react` | `*` |
79
79
 
80
80
  For Expo projects, rebuild native code after installation:
81
81
 
@@ -670,17 +670,25 @@ const freshToken = await AuthService.getAccessToken();
670
670
 
671
671
  ### Standardized Error Codes
672
672
 
673
- Handle failures reliably with predictable error strings. Some flows can surface provider-specific codes (listed below):
673
+ All errors thrown by `AuthService` and `useAuth` are `AuthError` instances with a type-safe `code` field (always a valid `AuthErrorCode`) and an optional `underlyingMessage` with the raw provider string when it differs from the code.
674
674
 
675
675
  ```ts
676
+ import { AuthError } from "react-native-nitro-auth";
677
+
676
678
  try {
677
679
  await login("google");
678
680
  } catch (e) {
679
- const error = e as Error;
680
- if (error.message === "cancelled") {
681
- // User closed the popup/picker
682
- } else if (error.message === "network_error") {
683
- // Connection issues
681
+ if (e instanceof AuthError) {
682
+ switch (e.code) {
683
+ case "cancelled":
684
+ // User closed the popup/picker
685
+ break;
686
+ case "network_error":
687
+ // Connection issues
688
+ break;
689
+ default:
690
+ console.error(e.code, e.underlyingMessage);
691
+ }
684
692
  }
685
693
  }
686
694
  ```
@@ -701,24 +709,24 @@ try {
701
709
 
702
710
  ### Native Error Metadata
703
711
 
704
- For more detailed debugging, Nitro Auth captures raw provider/native details in `underlyingError` where available:
712
+ `AuthError` carries the raw provider/native message in `underlyingMessage` when the platform error didn't map to a known code:
705
713
 
706
714
  ```ts
707
- // From authenticated user (on success)
708
- const { user } = useAuth();
709
- if (user?.underlyingError) {
710
- console.warn("Auth warning:", user.underlyingError);
711
- }
715
+ import { AuthError } from "react-native-nitro-auth";
712
716
 
713
- // From error (on failure)
714
717
  try {
715
718
  await login("google");
716
719
  } catch (e) {
717
- const error = e as Error & { underlyingError?: string };
718
- console.log("Native error:", error.underlyingError);
720
+ if (e instanceof AuthError) {
721
+ // e.code is always a valid AuthErrorCode
722
+ // e.underlyingMessage is the original native string (or undefined if it equalled the code)
723
+ console.log(e.code, e.underlyingMessage);
724
+ }
719
725
  }
720
726
  ```
721
727
 
728
+ The `AuthUser.underlyingError` field carries a raw warning string when the provider returns one alongside a successful result.
729
+
722
730
  ### Troubleshooting
723
731
 
724
732
  - `configuration_error`: verify client IDs, URL schemes, and redirect URIs are set for the current platform.
@@ -797,6 +805,9 @@ This is useful for scenarios where:
797
805
  ```ts
798
806
  import {
799
807
  AuthService,
808
+ AuthError,
809
+ isAuthErrorCode,
810
+ toAuthErrorCode,
800
811
  SocialButton,
801
812
  useAuth,
802
813
  type UseAuthReturn,
@@ -869,7 +880,7 @@ declare function useAuth(): UseAuthReturn;
869
880
  | `user` | `AuthUser \| undefined` | Current in-memory user |
870
881
  | `scopes` | `string[]` | Current granted scopes |
871
882
  | `loading` | `boolean` | `true` while an auth operation is in-flight |
872
- | `error` | `Error \| undefined` | Last operation error |
883
+ | `error` | `AuthError \| undefined` | Last operation error (typed, safe to switch on `.code`) |
873
884
  | `hasPlayServices` | `boolean` | Android Play Services availability |
874
885
  | `login` | `(provider: AuthProvider, options?: LoginOptions) => Promise<void>` | Starts provider login |
875
886
  | `logout` | `() => void` | Clears current session |
@@ -965,17 +976,36 @@ There is no public `setStorageAdapter`/`setJSStorageAdapter` API in this package
965
976
  | `nitroAuthWebStorage` | `"session" \| "local" \| "memory"` | `"session"` | Storage for non-sensitive web cache |
966
977
  | `nitroAuthPersistTokensOnWeb` | `boolean` | `false` | Persist sensitive tokens on web storage instead of memory |
967
978
 
968
- ### Error semantics
979
+ ### `AuthError`
980
+
981
+ All thrown errors from `AuthService` and `useAuth` are `AuthError` instances.
982
+
983
+ ```ts
984
+ class AuthError extends Error {
985
+ readonly code: AuthErrorCode; // Always a valid AuthErrorCode — safe to switch on
986
+ readonly underlyingMessage?: string; // Raw native/platform string when it differs from code
987
+ static from(e: unknown): AuthError; // Wraps any value; passes AuthError instances through
988
+ }
969
989
 
970
- Errors are surfaced as `Error` with `message` as a normalized code when possible, and `underlyingError` with provider/native details.
990
+ function isAuthErrorCode(value: string): value is AuthErrorCode;
991
+ function toAuthErrorCode(raw: string): AuthErrorCode; // Returns "unknown" for unrecognized strings
992
+ ```
971
993
 
972
- | Normalized message | Meaning |
973
- | --------------------- | ---------------------------------------------- |
974
- | `cancelled` | User cancelled popup/login flow |
975
- | `timeout` | Provider popup did not complete before timeout |
976
- | `popup_blocked` | Browser blocked popup opening |
977
- | `network_error` | Network failure |
978
- | `configuration_error` | Missing/invalid provider configuration |
994
+ | `code` | Meaning |
995
+ | ---------------------- | ---------------------------------------------- |
996
+ | `cancelled` | User cancelled the login flow |
997
+ | `timeout` | Provider popup did not complete before timeout |
998
+ | `popup_blocked` | Browser blocked popup opening |
999
+ | `network_error` | Network failure |
1000
+ | `configuration_error` | Missing/invalid provider configuration |
1001
+ | `unsupported_provider` | Provider not supported on this platform |
1002
+ | `invalid_state` | PKCE state mismatch (possible CSRF) |
1003
+ | `invalid_nonce` | Nonce mismatch in token response |
1004
+ | `token_error` | Token exchange failed |
1005
+ | `no_id_token` | No `id_token` in token response |
1006
+ | `parse_error` | Failed to parse token response |
1007
+ | `refresh_failed` | Refresh token flow failed |
1008
+ | `unknown` | Unrecognized error |
979
1009
 
980
1010
  ## Quality Gates
981
1011
 
@@ -4,5 +4,7 @@
4
4
 
5
5
  extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
6
6
  (void)reserved;
7
- return margelo::nitro::NitroAuth::initialize(vm);
7
+ return facebook::jni::initialize(vm, []() {
8
+ margelo::nitro::NitroAuth::registerAllNatives();
9
+ });
8
10
  }
@@ -351,19 +351,21 @@ extern "C" JNIEXPORT void JNICALL Java_com_auth_AuthAdapter_nativeOnLoginError(
351
351
  const char* errorCStr = env->GetStringUTFChars(error, nullptr);
352
352
  std::string errorStr(errorCStr);
353
353
  env->ReleaseStringUTFChars(error, errorCStr);
354
-
355
- std::string finalError = errorStr;
354
+
355
+ // errorStr is the structured AuthErrorCode (e.g. "cancelled", "network_error").
356
+ // underlyingError is a raw platform message for debugging — it must not replace the code.
356
357
  if (underlyingError) {
357
358
  const char* uCStr = env->GetStringUTFChars(underlyingError, nullptr);
358
- finalError = std::string(uCStr);
359
359
  env->ReleaseStringUTFChars(underlyingError, uCStr);
360
+ // underlyingError is intentionally discarded here; the structured code is sufficient
361
+ // for consumers. If richer debugging is needed, add it to the AuthUser.underlyingError field.
360
362
  }
361
363
 
362
- if (loginPromise) loginPromise->reject(std::make_exception_ptr(std::runtime_error(finalError)));
363
- if (scopesPromise) scopesPromise->reject(std::make_exception_ptr(std::runtime_error(finalError)));
364
+ if (loginPromise) loginPromise->reject(std::make_exception_ptr(std::runtime_error(errorStr)));
365
+ if (scopesPromise) scopesPromise->reject(std::make_exception_ptr(std::runtime_error(errorStr)));
364
366
  if (silentPromise) {
365
367
  if (errorStr == "No session") silentPromise->resolve(std::nullopt);
366
- else silentPromise->reject(std::make_exception_ptr(std::runtime_error(finalError)));
368
+ else silentPromise->reject(std::make_exception_ptr(std::runtime_error(errorStr)));
367
369
  }
368
370
  }
369
371
 
@@ -409,17 +411,15 @@ extern "C" JNIEXPORT void JNICALL Java_com_auth_AuthAdapter_nativeOnRefreshError
409
411
  gRefreshPromise = nullptr;
410
412
  }
411
413
  if (refreshPromise) {
412
- std::string finalError;
413
414
  const char* errorCStr = env->GetStringUTFChars(error, nullptr);
414
- finalError = std::string(errorCStr);
415
+ std::string errorStr(errorCStr);
415
416
  env->ReleaseStringUTFChars(error, errorCStr);
416
-
417
+
417
418
  if (underlyingError) {
418
419
  const char* uCStr = env->GetStringUTFChars(underlyingError, nullptr);
419
- finalError = std::string(uCStr);
420
420
  env->ReleaseStringUTFChars(underlyingError, uCStr);
421
421
  }
422
- refreshPromise->reject(std::make_exception_ptr(std::runtime_error(finalError)));
422
+ refreshPromise->reject(std::make_exception_ptr(std::runtime_error(errorStr)));
423
423
  }
424
424
  }
425
425