react-native-nitro-auth 0.5.5 → 0.5.6

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 (82) hide show
  1. package/README.md +22 -17
  2. package/android/proguard-rules.pro +7 -1
  3. package/android/src/main/AndroidManifest.xml +12 -0
  4. package/android/src/main/cpp/PlatformAuth+Android.cpp +260 -67
  5. package/android/src/main/java/com/auth/AuthAdapter.kt +217 -146
  6. package/android/src/main/java/com/auth/GoogleSignInActivity.kt +9 -5
  7. package/cpp/HybridAuth.cpp +79 -64
  8. package/cpp/HybridAuth.hpp +9 -7
  9. package/cpp/JSONSerializer.hpp +3 -0
  10. package/ios/AuthAdapter.swift +173 -60
  11. package/ios/PlatformAuth+iOS.mm +10 -3
  12. package/lib/commonjs/Auth.web.js +50 -10
  13. package/lib/commonjs/Auth.web.js.map +1 -1
  14. package/lib/commonjs/index.web.js +30 -12
  15. package/lib/commonjs/index.web.js.map +1 -1
  16. package/lib/commonjs/service.js +9 -7
  17. package/lib/commonjs/service.js.map +1 -1
  18. package/lib/commonjs/service.web.js +65 -13
  19. package/lib/commonjs/service.web.js.map +1 -1
  20. package/lib/commonjs/ui/social-button.js +19 -14
  21. package/lib/commonjs/ui/social-button.js.map +1 -1
  22. package/lib/commonjs/ui/social-button.web.js +16 -10
  23. package/lib/commonjs/ui/social-button.web.js.map +1 -1
  24. package/lib/commonjs/use-auth.js +13 -5
  25. package/lib/commonjs/use-auth.js.map +1 -1
  26. package/lib/commonjs/utils/logger.js +1 -0
  27. package/lib/commonjs/utils/logger.js.map +1 -1
  28. package/lib/module/Auth.web.js +50 -10
  29. package/lib/module/Auth.web.js.map +1 -1
  30. package/lib/module/global.d.js.map +1 -1
  31. package/lib/module/index.js.map +1 -1
  32. package/lib/module/index.web.js +2 -1
  33. package/lib/module/index.web.js.map +1 -1
  34. package/lib/module/service.js +9 -7
  35. package/lib/module/service.js.map +1 -1
  36. package/lib/module/service.web.js +65 -13
  37. package/lib/module/service.web.js.map +1 -1
  38. package/lib/module/ui/social-button.js +19 -14
  39. package/lib/module/ui/social-button.js.map +1 -1
  40. package/lib/module/ui/social-button.web.js +16 -10
  41. package/lib/module/ui/social-button.web.js.map +1 -1
  42. package/lib/module/use-auth.js +13 -5
  43. package/lib/module/use-auth.js.map +1 -1
  44. package/lib/module/utils/logger.js +1 -0
  45. package/lib/module/utils/logger.js.map +1 -1
  46. package/lib/typescript/commonjs/Auth.web.d.ts +5 -1
  47. package/lib/typescript/commonjs/Auth.web.d.ts.map +1 -1
  48. package/lib/typescript/commonjs/index.d.ts +1 -1
  49. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  50. package/lib/typescript/commonjs/index.web.d.ts +2 -1
  51. package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
  52. package/lib/typescript/commonjs/service.d.ts.map +1 -1
  53. package/lib/typescript/commonjs/service.web.d.ts +2 -18
  54. package/lib/typescript/commonjs/service.web.d.ts.map +1 -1
  55. package/lib/typescript/commonjs/ui/social-button.d.ts.map +1 -1
  56. package/lib/typescript/commonjs/ui/social-button.web.d.ts.map +1 -1
  57. package/lib/typescript/commonjs/use-auth.d.ts.map +1 -1
  58. package/lib/typescript/commonjs/utils/logger.d.ts.map +1 -1
  59. package/lib/typescript/module/Auth.web.d.ts +5 -1
  60. package/lib/typescript/module/Auth.web.d.ts.map +1 -1
  61. package/lib/typescript/module/index.d.ts +1 -1
  62. package/lib/typescript/module/index.d.ts.map +1 -1
  63. package/lib/typescript/module/index.web.d.ts +2 -1
  64. package/lib/typescript/module/index.web.d.ts.map +1 -1
  65. package/lib/typescript/module/service.d.ts.map +1 -1
  66. package/lib/typescript/module/service.web.d.ts +2 -18
  67. package/lib/typescript/module/service.web.d.ts.map +1 -1
  68. package/lib/typescript/module/ui/social-button.d.ts.map +1 -1
  69. package/lib/typescript/module/ui/social-button.web.d.ts.map +1 -1
  70. package/lib/typescript/module/use-auth.d.ts.map +1 -1
  71. package/lib/typescript/module/utils/logger.d.ts.map +1 -1
  72. package/package.json +2 -4
  73. package/src/Auth.web.ts +77 -11
  74. package/src/global.d.ts +0 -11
  75. package/src/index.ts +5 -1
  76. package/src/index.web.ts +6 -1
  77. package/src/service.ts +9 -7
  78. package/src/service.web.ts +84 -15
  79. package/src/ui/social-button.tsx +21 -9
  80. package/src/ui/social-button.web.tsx +17 -4
  81. package/src/use-auth.ts +35 -8
  82. package/src/utils/logger.ts +1 -0
package/src/service.ts CHANGED
@@ -29,7 +29,8 @@ export const AuthService: Auth = {
29
29
 
30
30
  async login(provider: AuthProvider, options?: LoginOptions) {
31
31
  try {
32
- return await nitroAuth.login(provider, options);
32
+ await nitroAuth.login(provider, options);
33
+ return;
33
34
  } catch (e) {
34
35
  throw AuthError.from(e);
35
36
  }
@@ -37,7 +38,8 @@ export const AuthService: Auth = {
37
38
 
38
39
  async requestScopes(scopes: string[]) {
39
40
  try {
40
- return await nitroAuth.requestScopes(scopes);
41
+ await nitroAuth.requestScopes(scopes);
42
+ return;
41
43
  } catch (e) {
42
44
  throw AuthError.from(e);
43
45
  }
@@ -45,7 +47,8 @@ export const AuthService: Auth = {
45
47
 
46
48
  async revokeScopes(scopes: string[]) {
47
49
  try {
48
- return await nitroAuth.revokeScopes(scopes);
50
+ await nitroAuth.revokeScopes(scopes);
51
+ return;
49
52
  } catch (e) {
50
53
  throw AuthError.from(e);
51
54
  }
@@ -73,16 +76,15 @@ export const AuthService: Auth = {
73
76
 
74
77
  async silentRestore() {
75
78
  try {
76
- return await nitroAuth.silentRestore();
79
+ await nitroAuth.silentRestore();
80
+ return;
77
81
  } catch (e) {
78
82
  throw AuthError.from(e);
79
83
  }
80
84
  },
81
85
 
82
86
  onAuthStateChanged(callback: (user: AuthUser | undefined) => void) {
83
- return nitroAuth.onAuthStateChanged((user) => {
84
- callback(user);
85
- });
87
+ return nitroAuth.onAuthStateChanged(callback);
86
88
  },
87
89
 
88
90
  onTokensRefreshed(callback: (tokens: AuthTokens) => void) {
@@ -1,8 +1,14 @@
1
+ import type {
2
+ Auth,
3
+ AuthProvider,
4
+ AuthTokens,
5
+ LoginOptions,
6
+ AuthUser,
7
+ } from "./Auth.nitro";
1
8
  import { AuthModule } from "./Auth.web";
9
+ import { AuthError } from "./utils/auth-error";
2
10
 
3
- export const AuthService = {
4
- ...AuthModule,
5
-
11
+ export const AuthService: Auth = {
6
12
  get name() {
7
13
  return AuthModule.name;
8
14
  },
@@ -19,16 +25,79 @@ export const AuthService = {
19
25
  return AuthModule.hasPlayServices;
20
26
  },
21
27
 
22
- login: AuthModule.login.bind(AuthModule),
23
- logout: AuthModule.logout.bind(AuthModule),
24
- requestScopes: AuthModule.requestScopes.bind(AuthModule),
25
- revokeScopes: AuthModule.revokeScopes.bind(AuthModule),
26
- getAccessToken: AuthModule.getAccessToken.bind(AuthModule),
27
- refreshToken: AuthModule.refreshToken.bind(AuthModule),
28
- silentRestore: AuthModule.silentRestore.bind(AuthModule),
29
- onAuthStateChanged: AuthModule.onAuthStateChanged.bind(AuthModule),
30
- onTokensRefreshed: AuthModule.onTokensRefreshed.bind(AuthModule),
31
- setLoggingEnabled: AuthModule.setLoggingEnabled.bind(AuthModule),
32
- dispose: AuthModule.dispose.bind(AuthModule),
33
- equals: AuthModule.equals.bind(AuthModule),
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
+ },
34
103
  };
@@ -7,8 +7,9 @@ import {
7
7
  View,
8
8
  ActivityIndicator,
9
9
  } from "react-native";
10
- import { NitroModules } from "react-native-nitro-modules";
11
- import type { Auth, AuthProvider, AuthUser } from "../Auth.nitro";
10
+ import { AuthService } from "../service";
11
+ import { logger } from "../utils/logger";
12
+ import type { AuthProvider, AuthUser } from "../Auth.nitro";
12
13
 
13
14
  export type SocialButtonVariant = "primary" | "outline" | "white" | "black";
14
15
 
@@ -56,8 +57,7 @@ const getTextColor = (variant: SocialButtonVariant): string =>
56
57
  variant === "white" || variant === "outline" ? "#000000" : "#FFFFFF";
57
58
 
58
59
  async function performLogin(provider: AuthProvider): Promise<void> {
59
- const auth = NitroModules.createHybridObject<Auth>("Auth");
60
- await auth.login(provider);
60
+ await AuthService.login(provider);
61
61
  }
62
62
 
63
63
  export const SocialButton = ({
@@ -83,12 +83,16 @@ export const SocialButton = ({
83
83
  setLoading(true);
84
84
  try {
85
85
  await performLogin(provider);
86
- const user = NitroModules.createHybridObject<Auth>("Auth").currentUser;
86
+ const user = AuthService.currentUser;
87
87
  if (user) {
88
88
  onSuccess?.(user);
89
89
  }
90
90
  } catch (error) {
91
- onError?.(error);
91
+ if (onError) {
92
+ onError(error);
93
+ } else if (__DEV__) {
94
+ logger.error("SocialButton unhandled error:", error);
95
+ }
92
96
  } finally {
93
97
  setLoading(false);
94
98
  }
@@ -126,19 +130,21 @@ export const SocialButton = ({
126
130
  <>
127
131
  {provider === "google" && variant !== "primary" && (
128
132
  <View style={styles.iconPlaceholder}>
129
- <Text style={{ fontSize: 18 }}>G</Text>
133
+ <Text style={styles.iconText}>G</Text>
130
134
  </View>
131
135
  )}
132
136
  {provider === "apple" && variant !== "primary" && (
133
137
  <View style={styles.iconPlaceholder}>
134
- <Text style={{ fontSize: 18, color: getTextColor(variant) }}>
138
+ <Text
139
+ style={[styles.iconText, { color: getTextColor(variant) }]}
140
+ >
135
141
 
136
142
  </Text>
137
143
  </View>
138
144
  )}
139
145
  {provider === "microsoft" && variant !== "primary" && (
140
146
  <View style={styles.iconPlaceholder}>
141
- <Text style={{ fontSize: 16 }}>⊞</Text>
147
+ <Text style={styles.microsoftIconText}>⊞</Text>
142
148
  </View>
143
149
  )}
144
150
  <Text
@@ -179,4 +185,10 @@ const styles = StyleSheet.create({
179
185
  fontSize: 16,
180
186
  fontWeight: "600",
181
187
  },
188
+ iconText: {
189
+ fontSize: 18,
190
+ },
191
+ microsoftIconText: {
192
+ fontSize: 16,
193
+ },
182
194
  });
@@ -8,6 +8,7 @@ import {
8
8
  ActivityIndicator,
9
9
  } from "react-native";
10
10
  import { AuthModule } from "../Auth.web";
11
+ import { logger } from "../utils/logger";
11
12
  import type { AuthProvider, AuthUser } from "../Auth.nitro";
12
13
 
13
14
  export type SocialButtonVariant = "primary" | "outline" | "white" | "black";
@@ -87,7 +88,11 @@ export const SocialButton = ({
87
88
  onSuccess?.(user);
88
89
  }
89
90
  } catch (error) {
90
- onError?.(error);
91
+ if (onError) {
92
+ onError(error);
93
+ } else if (process.env.NODE_ENV !== "production") {
94
+ logger.error("SocialButton unhandled error:", error);
95
+ }
91
96
  } finally {
92
97
  setLoading(false);
93
98
  }
@@ -125,19 +130,21 @@ export const SocialButton = ({
125
130
  <>
126
131
  {provider === "google" && variant !== "primary" && (
127
132
  <View style={styles.iconPlaceholder}>
128
- <Text style={{ fontSize: 18 }}>G</Text>
133
+ <Text style={styles.iconText}>G</Text>
129
134
  </View>
130
135
  )}
131
136
  {provider === "apple" && variant !== "primary" && (
132
137
  <View style={styles.iconPlaceholder}>
133
- <Text style={{ fontSize: 18, color: getTextColor(variant) }}>
138
+ <Text
139
+ style={[styles.iconText, { color: getTextColor(variant) }]}
140
+ >
134
141
 
135
142
  </Text>
136
143
  </View>
137
144
  )}
138
145
  {provider === "microsoft" && variant !== "primary" && (
139
146
  <View style={styles.iconPlaceholder}>
140
- <Text style={{ fontSize: 16 }}>⊞</Text>
147
+ <Text style={styles.microsoftIconText}>⊞</Text>
141
148
  </View>
142
149
  )}
143
150
  <Text
@@ -178,4 +185,10 @@ const styles = StyleSheet.create({
178
185
  fontSize: 16,
179
186
  fontWeight: "600",
180
187
  },
188
+ iconText: {
189
+ fontSize: 18,
190
+ },
191
+ microsoftIconText: {
192
+ fontSize: 16,
193
+ },
181
194
  });
package/src/use-auth.ts CHANGED
@@ -17,8 +17,10 @@ type AuthState = {
17
17
 
18
18
  const areScopesEqual = (left: string[], right: string[]): boolean => {
19
19
  if (left.length !== right.length) return false;
20
- for (let i = 0; i < left.length; i += 1) {
21
- if (left[i] !== right[i]) return false;
20
+ const sortedLeft = [...left].sort();
21
+ const sortedRight = [...right].sort();
22
+ for (let i = 0; i < sortedLeft.length; i += 1) {
23
+ if (sortedLeft[i] !== sortedRight[i]) return false;
22
24
  }
23
25
  return true;
24
26
  };
@@ -55,7 +57,12 @@ export function useAuth(): UseAuthReturn {
55
57
  ) {
56
58
  return prev;
57
59
  }
58
- return { user: nextUser, scopes: nextScopes, loading: nextLoading, error: nextError };
60
+ return {
61
+ user: nextUser,
62
+ scopes: nextScopes,
63
+ loading: nextLoading,
64
+ error: nextError,
65
+ };
59
66
  });
60
67
  },
61
68
  [],
@@ -139,7 +146,7 @@ export function useAuth(): UseAuthReturn {
139
146
  }, [syncStateFromService]);
140
147
 
141
148
  useEffect(() => {
142
- const unsubscribe = AuthService.onAuthStateChanged((currentUser) => {
149
+ const unsubscribeAuth = AuthService.onAuthStateChanged((currentUser) => {
143
150
  const nextScopes = AuthService.grantedScopes;
144
151
  setState((prev) => {
145
152
  if (
@@ -149,11 +156,22 @@ export function useAuth(): UseAuthReturn {
149
156
  ) {
150
157
  return prev;
151
158
  }
152
- return { ...prev, user: currentUser, scopes: nextScopes, loading: false };
159
+ return {
160
+ ...prev,
161
+ user: currentUser,
162
+ scopes: nextScopes,
163
+ loading: false,
164
+ };
153
165
  });
154
166
  });
155
- return unsubscribe;
156
- }, []);
167
+ const unsubscribeTokens = AuthService.onTokensRefreshed?.(() => {
168
+ syncStateFromService(false, undefined);
169
+ });
170
+ return () => {
171
+ unsubscribeAuth();
172
+ unsubscribeTokens?.();
173
+ };
174
+ }, [syncStateFromService]);
157
175
 
158
176
  return useMemo(
159
177
  () => ({
@@ -167,6 +185,15 @@ export function useAuth(): UseAuthReturn {
167
185
  refreshToken,
168
186
  silentRestore,
169
187
  }),
170
- [state, login, logout, requestScopes, revokeScopes, getAccessToken, refreshToken, silentRestore],
188
+ [
189
+ state,
190
+ login,
191
+ logout,
192
+ requestScopes,
193
+ revokeScopes,
194
+ getAccessToken,
195
+ refreshToken,
196
+ silentRestore,
197
+ ],
171
198
  );
172
199
  }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-console */
1
2
  let enabled = false;
2
3
 
3
4
  export const logger = {