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.
- package/README.md +50 -0
- package/android/build.gradle +7 -2
- package/android/src/main/cpp/PlatformAuth+Android.cpp +14 -3
- package/android/src/main/java/com/auth/AuthAdapter.kt +100 -3
- package/cpp/HybridAuth.cpp +58 -13
- package/cpp/HybridAuth.hpp +8 -0
- package/cpp/PlatformAuth.hpp +2 -1
- package/ios/PlatformAuth+iOS.mm +22 -4
- package/lib/commonjs/Auth.web.js +48 -10
- package/lib/commonjs/Auth.web.js.map +1 -1
- package/lib/commonjs/AuthStorage.nitro.js +6 -0
- package/lib/commonjs/AuthStorage.nitro.js.map +1 -0
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +12 -0
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/module/Auth.web.js +48 -10
- package/lib/module/Auth.web.js.map +1 -1
- package/lib/module/AuthStorage.nitro.js +4 -0
- package/lib/module/AuthStorage.nitro.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +1 -0
- package/lib/module/index.web.js.map +1 -1
- package/lib/typescript/Auth.nitro.d.ts +4 -0
- package/lib/typescript/Auth.nitro.d.ts.map +1 -1
- package/lib/typescript/Auth.web.d.ts +8 -1
- package/lib/typescript/Auth.web.d.ts.map +1 -1
- package/lib/typescript/AuthStorage.nitro.d.ts +19 -0
- package/lib/typescript/AuthStorage.nitro.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +1 -0
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/nitrogen/generated/android/NitroAuth+autolinking.cmake +1 -0
- package/nitrogen/generated/shared/c++/HybridAuthSpec.cpp +2 -0
- package/nitrogen/generated/shared/c++/HybridAuthSpec.hpp +6 -0
- package/nitrogen/generated/shared/c++/HybridAuthStorageAdapterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridAuthStorageAdapterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/LoginOptions.hpp +6 -2
- package/package.json +1 -1
- package/react-native-nitro-auth.podspec +1 -1
- package/src/Auth.nitro.ts +5 -0
- package/src/Auth.web.ts +71 -11
- package/src/AuthStorage.nitro.ts +17 -0
- package/src/index.ts +1 -0
- package/src/index.web.ts +1 -0
package/src/Auth.web.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type {
|
|
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
|
-
|
|
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
|
-
|
|
44
|
+
this.removeFromCache(CACHE_KEY);
|
|
29
45
|
}
|
|
30
46
|
}
|
|
31
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
319
|
-
|
|
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
|
-
|
|
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