react-native-nitro-auth 0.1.4 → 0.1.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 (47) hide show
  1. package/README.md +50 -0
  2. package/android/build.gradle +7 -2
  3. package/android/src/main/cpp/PlatformAuth+Android.cpp +14 -3
  4. package/android/src/main/java/com/auth/AuthAdapter.kt +100 -3
  5. package/cpp/HybridAuth.cpp +58 -13
  6. package/cpp/HybridAuth.hpp +8 -0
  7. package/cpp/PlatformAuth.hpp +2 -1
  8. package/ios/PlatformAuth+iOS.mm +22 -4
  9. package/lib/commonjs/Auth.web.js +48 -10
  10. package/lib/commonjs/Auth.web.js.map +1 -1
  11. package/lib/commonjs/AuthStorage.nitro.js +6 -0
  12. package/lib/commonjs/AuthStorage.nitro.js.map +1 -0
  13. package/lib/commonjs/index.js +12 -0
  14. package/lib/commonjs/index.js.map +1 -1
  15. package/lib/commonjs/index.web.js +12 -0
  16. package/lib/commonjs/index.web.js.map +1 -1
  17. package/lib/module/Auth.web.js +48 -10
  18. package/lib/module/Auth.web.js.map +1 -1
  19. package/lib/module/AuthStorage.nitro.js +4 -0
  20. package/lib/module/AuthStorage.nitro.js.map +1 -0
  21. package/lib/module/index.js +1 -0
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/index.web.js +1 -0
  24. package/lib/module/index.web.js.map +1 -1
  25. package/lib/typescript/Auth.nitro.d.ts +4 -0
  26. package/lib/typescript/Auth.nitro.d.ts.map +1 -1
  27. package/lib/typescript/Auth.web.d.ts +8 -1
  28. package/lib/typescript/Auth.web.d.ts.map +1 -1
  29. package/lib/typescript/AuthStorage.nitro.d.ts +19 -0
  30. package/lib/typescript/AuthStorage.nitro.d.ts.map +1 -0
  31. package/lib/typescript/index.d.ts +1 -0
  32. package/lib/typescript/index.d.ts.map +1 -1
  33. package/lib/typescript/index.web.d.ts +1 -0
  34. package/lib/typescript/index.web.d.ts.map +1 -1
  35. package/nitrogen/generated/android/NitroAuth+autolinking.cmake +1 -0
  36. package/nitrogen/generated/shared/c++/HybridAuthSpec.cpp +2 -0
  37. package/nitrogen/generated/shared/c++/HybridAuthSpec.hpp +6 -0
  38. package/nitrogen/generated/shared/c++/HybridAuthStorageAdapterSpec.cpp +23 -0
  39. package/nitrogen/generated/shared/c++/HybridAuthStorageAdapterSpec.hpp +65 -0
  40. package/nitrogen/generated/shared/c++/LoginOptions.hpp +6 -2
  41. package/package.json +1 -1
  42. package/react-native-nitro-auth.podspec +1 -1
  43. package/src/Auth.nitro.ts +5 -0
  44. package/src/Auth.web.ts +71 -11
  45. package/src/AuthStorage.nitro.ts +17 -0
  46. package/src/index.ts +1 -0
  47. package/src/index.web.ts +1 -0
package/src/Auth.web.ts CHANGED
@@ -1,4 +1,11 @@
1
- import type { Auth, AuthUser, AuthProvider, LoginOptions } from "./Auth.nitro";
1
+ import type {
2
+ Auth,
3
+ AuthUser,
4
+ AuthProvider,
5
+ LoginOptions,
6
+ AuthTokens,
7
+ } from "./Auth.nitro";
8
+ import type { AuthStorageAdapter } from "./AuthStorage.nitro";
2
9
  import { logger } from "./utils/logger";
3
10
 
4
11
  const CACHE_KEY = "nitro_auth_user";
@@ -18,26 +25,47 @@ class AuthWeb implements Auth {
18
25
  private _currentUser: AuthUser | undefined;
19
26
  private _grantedScopes: string[] = [];
20
27
  private _listeners: ((user: AuthUser | undefined) => void)[] = [];
28
+ private _tokenListeners: ((tokens: AuthTokens) => void)[] = [];
29
+ private _storageAdapter: AuthStorageAdapter | undefined;
21
30
 
22
31
  constructor() {
23
- const cached = localStorage.getItem(CACHE_KEY);
32
+ this.loadFromCache();
33
+ }
34
+
35
+ private loadFromCache() {
36
+ const cached = this._storageAdapter
37
+ ? this._storageAdapter.load(CACHE_KEY)
38
+ : localStorage.getItem(CACHE_KEY);
39
+
24
40
  if (cached) {
25
41
  try {
26
42
  this._currentUser = JSON.parse(cached);
27
43
  } catch {
28
- localStorage.removeItem(CACHE_KEY);
44
+ this.removeFromCache(CACHE_KEY);
29
45
  }
30
46
  }
31
- const scopes = localStorage.getItem(SCOPES_KEY);
47
+
48
+ const scopes = this._storageAdapter
49
+ ? this._storageAdapter.load(SCOPES_KEY)
50
+ : localStorage.getItem(SCOPES_KEY);
51
+
32
52
  if (scopes) {
33
53
  try {
34
54
  this._grantedScopes = JSON.parse(scopes);
35
55
  } catch {
36
- localStorage.removeItem(SCOPES_KEY);
56
+ this.removeFromCache(SCOPES_KEY);
37
57
  }
38
58
  }
39
59
  }
40
60
 
61
+ private removeFromCache(key: string) {
62
+ if (this._storageAdapter) {
63
+ this._storageAdapter.remove(key);
64
+ } else {
65
+ localStorage.removeItem(key);
66
+ }
67
+ }
68
+
41
69
  get currentUser(): AuthUser | undefined {
42
70
  return this._currentUser;
43
71
  }
@@ -60,6 +88,13 @@ class AuthWeb implements Auth {
60
88
  };
61
89
  }
62
90
 
91
+ onTokensRefreshed(callback: (tokens: AuthTokens) => void): () => void {
92
+ this._tokenListeners.push(callback);
93
+ return () => {
94
+ this._tokenListeners = this._tokenListeners.filter((l) => l !== callback);
95
+ };
96
+ }
97
+
63
98
  private notify() {
64
99
  this._listeners.forEach((l) => l(this._currentUser));
65
100
  }
@@ -103,7 +138,14 @@ class AuthWeb implements Auth {
103
138
  this._grantedScopes = this._grantedScopes.filter(
104
139
  (s) => !scopes.includes(s)
105
140
  );
106
- localStorage.setItem(SCOPES_KEY, JSON.stringify(this._grantedScopes));
141
+ if (this._storageAdapter) {
142
+ this._storageAdapter.save(
143
+ SCOPES_KEY,
144
+ JSON.stringify(this._grantedScopes)
145
+ );
146
+ } else {
147
+ localStorage.setItem(SCOPES_KEY, JSON.stringify(this._grantedScopes));
148
+ }
107
149
  if (this._currentUser) {
108
150
  this._currentUser.scopes = this._grantedScopes;
109
151
  this.updateUser(this._currentUser);
@@ -132,10 +174,12 @@ class AuthWeb implements Auth {
132
174
  await this.loginGoogle(
133
175
  this._grantedScopes.length > 0 ? this._grantedScopes : DEFAULT_SCOPES
134
176
  );
135
- return {
177
+ const tokens = {
136
178
  accessToken: this._currentUser.accessToken,
137
179
  idToken: this._currentUser.idToken,
138
180
  };
181
+ this._tokenListeners.forEach((l) => l(tokens));
182
+ return tokens;
139
183
  }
140
184
 
141
185
  private mapError(error: unknown): Error {
@@ -227,7 +271,11 @@ class AuthWeb implements Auth {
227
271
 
228
272
  if (idToken) {
229
273
  this._grantedScopes = scopes;
230
- localStorage.setItem(SCOPES_KEY, JSON.stringify(scopes));
274
+ if (this._storageAdapter) {
275
+ this._storageAdapter.save(SCOPES_KEY, JSON.stringify(scopes));
276
+ } else {
277
+ localStorage.setItem(SCOPES_KEY, JSON.stringify(scopes));
278
+ }
231
279
 
232
280
  const user: AuthUser = {
233
281
  provider: "google",
@@ -315,14 +363,18 @@ class AuthWeb implements Auth {
315
363
  logout(): void {
316
364
  this._currentUser = undefined;
317
365
  this._grantedScopes = [];
318
- localStorage.removeItem(CACHE_KEY);
319
- localStorage.removeItem(SCOPES_KEY);
366
+ this.removeFromCache(CACHE_KEY);
367
+ this.removeFromCache(SCOPES_KEY);
320
368
  this.notify();
321
369
  }
322
370
 
323
371
  private updateUser(user: AuthUser) {
324
372
  this._currentUser = user;
325
- localStorage.setItem(CACHE_KEY, JSON.stringify(user));
373
+ if (this._storageAdapter) {
374
+ this._storageAdapter.save(CACHE_KEY, JSON.stringify(user));
375
+ } else {
376
+ localStorage.setItem(CACHE_KEY, JSON.stringify(user));
377
+ }
326
378
  this.notify();
327
379
  }
328
380
 
@@ -330,6 +382,14 @@ class AuthWeb implements Auth {
330
382
  logger.setEnabled(enabled);
331
383
  }
332
384
 
385
+ setStorageAdapter(adapter: AuthStorageAdapter | undefined): void {
386
+ this._storageAdapter = adapter;
387
+ if (adapter) {
388
+ this.loadFromCache();
389
+ this.notify();
390
+ }
391
+ }
392
+
333
393
  name = "Auth";
334
394
  dispose() {}
335
395
  equals(other: any) {
@@ -0,0 +1,17 @@
1
+ import type { HybridObject } from "react-native-nitro-modules";
2
+
3
+ export interface AuthStorageAdapter
4
+ extends HybridObject<{ ios: "c++"; android: "c++" }> {
5
+ /**
6
+ * Called to save a value to the custom storage.
7
+ */
8
+ save(key: string, value: string): void;
9
+ /**
10
+ * Called to load a value from the custom storage.
11
+ */
12
+ load(key: string): string | undefined;
13
+ /**
14
+ * Called to remove a value from the custom storage.
15
+ */
16
+ remove(key: string): void;
17
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./Auth.nitro";
2
+ export * from "./AuthStorage.nitro";
2
3
  export * from "./ui/social-button";
3
4
  export * from "./use-auth";
4
5
  export { AuthService } from "./service";
package/src/index.web.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./Auth.nitro";
2
+ export * from "./AuthStorage.nitro";
2
3
  export * from "./ui/social-button.web";
3
4
  export * from "./use-auth";
4
5
  export { AuthService } from "./service.web";