react-native-nitro-auth 0.5.8 → 0.5.10
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/CHANGELOG.md +48 -0
- package/README.md +69 -50
- package/cpp/HybridAuth.cpp +135 -50
- package/cpp/HybridAuth.hpp +1 -0
- package/ios/AuthAdapter.swift +28 -9
- package/lib/commonjs/Auth.web.js +141 -73
- package/lib/commonjs/Auth.web.js.map +1 -1
- package/lib/commonjs/create-auth-service.js +71 -0
- package/lib/commonjs/create-auth-service.js.map +1 -0
- package/lib/commonjs/service.js +2 -79
- package/lib/commonjs/service.js.map +1 -1
- package/lib/commonjs/service.web.js +2 -79
- package/lib/commonjs/service.web.js.map +1 -1
- package/lib/commonjs/use-auth.js +6 -3
- package/lib/commonjs/use-auth.js.map +1 -1
- package/lib/module/Auth.web.js +141 -73
- package/lib/module/Auth.web.js.map +1 -1
- package/lib/module/create-auth-service.js +67 -0
- package/lib/module/create-auth-service.js.map +1 -0
- package/lib/module/service.js +2 -79
- package/lib/module/service.js.map +1 -1
- package/lib/module/service.web.js +2 -79
- package/lib/module/service.web.js.map +1 -1
- package/lib/module/use-auth.js +6 -3
- package/lib/module/use-auth.js.map +1 -1
- package/lib/typescript/commonjs/Auth.web.d.ts +4 -2
- package/lib/typescript/commonjs/Auth.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/create-auth-service.d.ts +5 -0
- package/lib/typescript/commonjs/create-auth-service.d.ts.map +1 -0
- package/lib/typescript/commonjs/service.d.ts.map +1 -1
- package/lib/typescript/commonjs/service.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/use-auth.d.ts.map +1 -1
- package/lib/typescript/module/Auth.web.d.ts +4 -2
- package/lib/typescript/module/Auth.web.d.ts.map +1 -1
- package/lib/typescript/module/create-auth-service.d.ts +5 -0
- package/lib/typescript/module/create-auth-service.d.ts.map +1 -0
- package/lib/typescript/module/service.d.ts.map +1 -1
- package/lib/typescript/module/service.web.d.ts.map +1 -1
- package/lib/typescript/module/use-auth.d.ts.map +1 -1
- package/package.json +7 -5
- package/react-native-nitro-auth.podspec +1 -0
- package/src/Auth.web.ts +261 -102
- package/src/create-auth-service.ts +97 -0
- package/src/service.ts +3 -101
- package/src/service.web.ts +3 -101
- package/src/use-auth.ts +7 -3
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Auth,
|
|
3
|
+
AuthProvider,
|
|
4
|
+
AuthTokens,
|
|
5
|
+
LoginOptions,
|
|
6
|
+
AuthUser,
|
|
7
|
+
} from "./Auth.nitro";
|
|
8
|
+
import { AuthError } from "./utils/auth-error";
|
|
9
|
+
|
|
10
|
+
type AuthSource = () => Auth;
|
|
11
|
+
type AuthWithOptionalNativeMembers = Auth & {
|
|
12
|
+
onAuthStateChanged?: (
|
|
13
|
+
callback: (user: AuthUser | undefined) => void,
|
|
14
|
+
) => () => void;
|
|
15
|
+
onTokensRefreshed?: (callback: (tokens: AuthTokens) => void) => () => void;
|
|
16
|
+
setLoggingEnabled?: (enabled: boolean) => void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
async function wrapAuthOperation<T>(operation: () => Promise<T>): Promise<T> {
|
|
20
|
+
try {
|
|
21
|
+
return await operation();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
throw AuthError.from(e);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createAuthService(getAuth: AuthSource): Auth {
|
|
28
|
+
return {
|
|
29
|
+
get name() {
|
|
30
|
+
return getAuth().name;
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
get currentUser() {
|
|
34
|
+
return getAuth().currentUser;
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
get grantedScopes() {
|
|
38
|
+
const scopes = getAuth().grantedScopes;
|
|
39
|
+
return Array.isArray(scopes) ? scopes : [];
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
get hasPlayServices() {
|
|
43
|
+
return getAuth().hasPlayServices;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
login(provider: AuthProvider, options?: LoginOptions) {
|
|
47
|
+
return wrapAuthOperation(() => getAuth().login(provider, options));
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
requestScopes(scopes: string[]) {
|
|
51
|
+
return wrapAuthOperation(() => getAuth().requestScopes(scopes));
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
revokeScopes(scopes: string[]) {
|
|
55
|
+
return wrapAuthOperation(() => getAuth().revokeScopes(scopes));
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
getAccessToken() {
|
|
59
|
+
return wrapAuthOperation(() => getAuth().getAccessToken());
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
refreshToken() {
|
|
63
|
+
return wrapAuthOperation(() => getAuth().refreshToken());
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
logout() {
|
|
67
|
+
getAuth().logout();
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
silentRestore() {
|
|
71
|
+
return wrapAuthOperation(() => getAuth().silentRestore());
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
onAuthStateChanged(callback: (user: AuthUser | undefined) => void) {
|
|
75
|
+
const auth = getAuth() as AuthWithOptionalNativeMembers;
|
|
76
|
+
return auth.onAuthStateChanged?.(callback) ?? (() => {});
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
onTokensRefreshed(callback: (tokens: AuthTokens) => void) {
|
|
80
|
+
const auth = getAuth() as AuthWithOptionalNativeMembers;
|
|
81
|
+
return auth.onTokensRefreshed?.(callback) ?? (() => {});
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
setLoggingEnabled(enabled: boolean) {
|
|
85
|
+
const auth = getAuth() as AuthWithOptionalNativeMembers;
|
|
86
|
+
auth.setLoggingEnabled?.(enabled);
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
dispose() {
|
|
90
|
+
getAuth().dispose();
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
equals(other: Parameters<Auth["equals"]>[0]): boolean {
|
|
94
|
+
return getAuth().equals(other);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
package/src/service.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { NitroModules } from "react-native-nitro-modules";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
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);
|
package/src/service.web.ts
CHANGED
|
@@ -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 {
|
|
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 &&
|