react-native-nitro-auth 0.5.7 → 0.5.9

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 (54) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +295 -906
  3. package/android/src/main/java/com/auth/AuthAdapter.kt +22 -6
  4. package/cpp/HybridAuth.cpp +175 -39
  5. package/cpp/HybridAuth.hpp +2 -0
  6. package/ios/AuthAdapter.swift +2 -2
  7. package/lib/commonjs/Auth.web.js +141 -73
  8. package/lib/commonjs/Auth.web.js.map +1 -1
  9. package/lib/commonjs/create-auth-service.js +71 -0
  10. package/lib/commonjs/create-auth-service.js.map +1 -0
  11. package/lib/commonjs/service.js +2 -79
  12. package/lib/commonjs/service.js.map +1 -1
  13. package/lib/commonjs/service.web.js +2 -79
  14. package/lib/commonjs/service.web.js.map +1 -1
  15. package/lib/commonjs/use-auth.js +6 -3
  16. package/lib/commonjs/use-auth.js.map +1 -1
  17. package/lib/commonjs/utils/auth-error.js +8 -1
  18. package/lib/commonjs/utils/auth-error.js.map +1 -1
  19. package/lib/module/Auth.web.js +141 -73
  20. package/lib/module/Auth.web.js.map +1 -1
  21. package/lib/module/create-auth-service.js +67 -0
  22. package/lib/module/create-auth-service.js.map +1 -0
  23. package/lib/module/service.js +2 -79
  24. package/lib/module/service.js.map +1 -1
  25. package/lib/module/service.web.js +2 -79
  26. package/lib/module/service.web.js.map +1 -1
  27. package/lib/module/use-auth.js +6 -3
  28. package/lib/module/use-auth.js.map +1 -1
  29. package/lib/module/utils/auth-error.js +8 -1
  30. package/lib/module/utils/auth-error.js.map +1 -1
  31. package/lib/typescript/commonjs/Auth.web.d.ts +4 -2
  32. package/lib/typescript/commonjs/Auth.web.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/create-auth-service.d.ts +5 -0
  34. package/lib/typescript/commonjs/create-auth-service.d.ts.map +1 -0
  35. package/lib/typescript/commonjs/service.d.ts.map +1 -1
  36. package/lib/typescript/commonjs/service.web.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/use-auth.d.ts.map +1 -1
  38. package/lib/typescript/commonjs/utils/auth-error.d.ts.map +1 -1
  39. package/lib/typescript/module/Auth.web.d.ts +4 -2
  40. package/lib/typescript/module/Auth.web.d.ts.map +1 -1
  41. package/lib/typescript/module/create-auth-service.d.ts +5 -0
  42. package/lib/typescript/module/create-auth-service.d.ts.map +1 -0
  43. package/lib/typescript/module/service.d.ts.map +1 -1
  44. package/lib/typescript/module/service.web.d.ts.map +1 -1
  45. package/lib/typescript/module/use-auth.d.ts.map +1 -1
  46. package/lib/typescript/module/utils/auth-error.d.ts.map +1 -1
  47. package/package.json +12 -9
  48. package/react-native-nitro-auth.podspec +1 -0
  49. package/src/Auth.web.ts +261 -102
  50. package/src/create-auth-service.ts +97 -0
  51. package/src/service.ts +3 -101
  52. package/src/service.web.ts +3 -101
  53. package/src/use-auth.ts +7 -3
  54. package/src/utils/auth-error.ts +10 -1
package/src/service.ts CHANGED
@@ -1,12 +1,6 @@
1
1
  import { NitroModules } from "react-native-nitro-modules";
2
- import type {
3
- Auth,
4
- AuthProvider,
5
- AuthTokens,
6
- LoginOptions,
7
- AuthUser,
8
- } from "./Auth.nitro";
9
- import { AuthError } from "./utils/auth-error";
2
+ import type { Auth } from "./Auth.nitro";
3
+ import { createAuthService } from "./create-auth-service";
10
4
 
11
5
  let nitroAuth: Auth | undefined;
12
6
 
@@ -15,96 +9,4 @@ function getNitroAuth(): Auth {
15
9
  return nitroAuth;
16
10
  }
17
11
 
18
- export const AuthService: Auth = {
19
- get name() {
20
- return getNitroAuth().name;
21
- },
22
-
23
- get currentUser() {
24
- return getNitroAuth().currentUser;
25
- },
26
-
27
- get grantedScopes() {
28
- return getNitroAuth().grantedScopes;
29
- },
30
-
31
- get hasPlayServices() {
32
- return getNitroAuth().hasPlayServices;
33
- },
34
-
35
- async login(provider: AuthProvider, options?: LoginOptions) {
36
- try {
37
- await getNitroAuth().login(provider, options);
38
- return;
39
- } catch (e) {
40
- throw AuthError.from(e);
41
- }
42
- },
43
-
44
- async requestScopes(scopes: string[]) {
45
- try {
46
- await getNitroAuth().requestScopes(scopes);
47
- return;
48
- } catch (e) {
49
- throw AuthError.from(e);
50
- }
51
- },
52
-
53
- async revokeScopes(scopes: string[]) {
54
- try {
55
- await getNitroAuth().revokeScopes(scopes);
56
- return;
57
- } catch (e) {
58
- throw AuthError.from(e);
59
- }
60
- },
61
-
62
- async getAccessToken() {
63
- try {
64
- return await getNitroAuth().getAccessToken();
65
- } catch (e) {
66
- throw AuthError.from(e);
67
- }
68
- },
69
-
70
- async refreshToken() {
71
- try {
72
- return await getNitroAuth().refreshToken();
73
- } catch (e) {
74
- throw AuthError.from(e);
75
- }
76
- },
77
-
78
- logout() {
79
- getNitroAuth().logout();
80
- },
81
-
82
- async silentRestore() {
83
- try {
84
- await getNitroAuth().silentRestore();
85
- return;
86
- } catch (e) {
87
- throw AuthError.from(e);
88
- }
89
- },
90
-
91
- onAuthStateChanged(callback: (user: AuthUser | undefined) => void) {
92
- return getNitroAuth().onAuthStateChanged(callback);
93
- },
94
-
95
- onTokensRefreshed(callback: (tokens: AuthTokens) => void) {
96
- return getNitroAuth().onTokensRefreshed(callback);
97
- },
98
-
99
- setLoggingEnabled(enabled: boolean) {
100
- getNitroAuth().setLoggingEnabled(enabled);
101
- },
102
-
103
- dispose() {
104
- getNitroAuth().dispose();
105
- },
106
-
107
- equals(other: Parameters<Auth["equals"]>[0]): boolean {
108
- return getNitroAuth().equals(other);
109
- },
110
- };
12
+ export const AuthService: Auth = createAuthService(getNitroAuth);
@@ -1,103 +1,5 @@
1
- import type {
2
- Auth,
3
- AuthProvider,
4
- AuthTokens,
5
- LoginOptions,
6
- AuthUser,
7
- } from "./Auth.nitro";
1
+ import type { Auth } from "./Auth.nitro";
8
2
  import { AuthModule } from "./Auth.web";
9
- import { AuthError } from "./utils/auth-error";
3
+ import { createAuthService } from "./create-auth-service";
10
4
 
11
- export const AuthService: Auth = {
12
- get name() {
13
- return AuthModule.name;
14
- },
15
-
16
- get currentUser() {
17
- return AuthModule.currentUser;
18
- },
19
-
20
- get grantedScopes() {
21
- return AuthModule.grantedScopes;
22
- },
23
-
24
- get hasPlayServices() {
25
- return AuthModule.hasPlayServices;
26
- },
27
-
28
- async login(provider: AuthProvider, options?: LoginOptions) {
29
- try {
30
- await AuthModule.login(provider, options);
31
- return;
32
- } catch (e) {
33
- throw AuthError.from(e);
34
- }
35
- },
36
-
37
- async requestScopes(scopes: string[]) {
38
- try {
39
- await AuthModule.requestScopes(scopes);
40
- return;
41
- } catch (e) {
42
- throw AuthError.from(e);
43
- }
44
- },
45
-
46
- async revokeScopes(scopes: string[]) {
47
- try {
48
- await AuthModule.revokeScopes(scopes);
49
- return;
50
- } catch (e) {
51
- throw AuthError.from(e);
52
- }
53
- },
54
-
55
- async getAccessToken() {
56
- try {
57
- return await AuthModule.getAccessToken();
58
- } catch (e) {
59
- throw AuthError.from(e);
60
- }
61
- },
62
-
63
- async refreshToken() {
64
- try {
65
- return await AuthModule.refreshToken();
66
- } catch (e) {
67
- throw AuthError.from(e);
68
- }
69
- },
70
-
71
- logout() {
72
- AuthModule.logout();
73
- },
74
-
75
- async silentRestore() {
76
- try {
77
- await AuthModule.silentRestore();
78
- return;
79
- } catch (e) {
80
- throw AuthError.from(e);
81
- }
82
- },
83
-
84
- onAuthStateChanged(callback: (user: AuthUser | undefined) => void) {
85
- return AuthModule.onAuthStateChanged(callback);
86
- },
87
-
88
- onTokensRefreshed(callback: (tokens: AuthTokens) => void) {
89
- return AuthModule.onTokensRefreshed(callback);
90
- },
91
-
92
- setLoggingEnabled(enabled: boolean) {
93
- AuthModule.setLoggingEnabled(enabled);
94
- },
95
-
96
- dispose() {
97
- AuthModule.dispose();
98
- },
99
-
100
- equals(other: Parameters<Auth["equals"]>[0]): boolean {
101
- return AuthModule.equals(other);
102
- },
103
- };
5
+ export const AuthService: Auth = createAuthService(() => AuthModule);
package/src/use-auth.ts CHANGED
@@ -10,6 +10,10 @@ import { AuthError } from "./utils/auth-error";
10
10
 
11
11
  const EMPTY_SCOPES: string[] = [];
12
12
 
13
+ function normalizeScopes(scopes: string[] | undefined): string[] {
14
+ return Array.isArray(scopes) ? scopes : EMPTY_SCOPES;
15
+ }
16
+
13
17
  type AuthState = {
14
18
  user: AuthUser | undefined;
15
19
  scopes: string[];
@@ -53,7 +57,7 @@ export type UseAuthReturn = AuthState & {
53
57
  export function useAuth(): UseAuthReturn {
54
58
  const [state, setState] = useState<AuthState>({
55
59
  user: AuthService.currentUser,
56
- scopes: AuthService.grantedScopes,
60
+ scopes: normalizeScopes(AuthService.grantedScopes),
57
61
  loading: false,
58
62
  error: undefined,
59
63
  });
@@ -61,7 +65,7 @@ export function useAuth(): UseAuthReturn {
61
65
  const syncStateFromService = useCallback(
62
66
  (nextLoading: boolean, nextError: AuthError | undefined) => {
63
67
  const nextUser = AuthService.currentUser;
64
- const nextScopes = AuthService.grantedScopes;
68
+ const nextScopes = normalizeScopes(AuthService.grantedScopes);
65
69
  setState((prev) => {
66
70
  if (
67
71
  prev.loading === nextLoading &&
@@ -176,7 +180,7 @@ export function useAuth(): UseAuthReturn {
176
180
 
177
181
  useEffect(() => {
178
182
  const unsubscribeAuth = AuthService.onAuthStateChanged((currentUser) => {
179
- const nextScopes = AuthService.grantedScopes;
183
+ const nextScopes = normalizeScopes(AuthService.grantedScopes);
180
184
  setState((prev) => {
181
185
  if (
182
186
  prev.user === currentUser &&
@@ -23,7 +23,16 @@ export function isAuthErrorCode(value: string): value is AuthErrorCode {
23
23
  }
24
24
 
25
25
  export function toAuthErrorCode(raw: string): AuthErrorCode {
26
- return isAuthErrorCode(raw) ? raw : "unknown";
26
+ if (isAuthErrorCode(raw)) {
27
+ return raw;
28
+ }
29
+
30
+ const prefix = raw.split(":", 1)[0]?.trim();
31
+ if (prefix && isAuthErrorCode(prefix)) {
32
+ return prefix;
33
+ }
34
+
35
+ return "unknown";
27
36
  }
28
37
 
29
38
  /**