@ic-reactor/react 0.4.3 → 0.4.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/dist/hooks/auth.d.ts +4 -4
- package/dist/hooks/auth.js +53 -34
- package/dist/index.d.ts +2 -2
- package/dist/types.d.ts +6 -6
- package/package.json +2 -2
package/dist/hooks/auth.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
2
|
-
import type { AgentManager } from "@ic-reactor/store";
|
|
2
|
+
import type { AgentManager, Identity } from "@ic-reactor/store";
|
|
3
3
|
import { AuthArgs } from "../types";
|
|
4
4
|
export type AuthHooks = ReturnType<typeof getAuthHooks>;
|
|
5
5
|
export declare const getAuthHooks: (agentManager: AgentManager) => {
|
|
@@ -9,13 +9,13 @@ export declare const getAuthHooks: (agentManager: AgentManager) => {
|
|
|
9
9
|
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
10
10
|
authenticated: boolean;
|
|
11
11
|
authenticating: boolean;
|
|
12
|
-
identity:
|
|
12
|
+
identity: Identity | null;
|
|
13
13
|
login: (options?: AuthClientLoginOptions) => Promise<void>;
|
|
14
14
|
logout: (options?: {
|
|
15
15
|
returnTo?: string;
|
|
16
16
|
}) => Promise<void>;
|
|
17
|
-
authenticate: () => Promise<
|
|
17
|
+
authenticate: () => Promise<Identity>;
|
|
18
18
|
loginLoading: boolean;
|
|
19
|
-
loginError:
|
|
19
|
+
loginError: Error | null;
|
|
20
20
|
};
|
|
21
21
|
};
|
package/dist/hooks/auth.js
CHANGED
|
@@ -26,14 +26,19 @@ const getAuthHooks = (agentManager) => {
|
|
|
26
26
|
const [loginError, setLoginError] = (0, react_1.useState)(null);
|
|
27
27
|
const { authClient, authenticated, authenticating, identity } = useAuthStore();
|
|
28
28
|
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
const authenticatePromise = new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
try {
|
|
31
|
+
const identity = yield authenticator();
|
|
32
|
+
onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
|
|
33
|
+
resolve(identity);
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
|
|
41
|
+
return authenticatePromise;
|
|
37
42
|
}), [
|
|
38
43
|
authenticator,
|
|
39
44
|
onAuthentication,
|
|
@@ -43,33 +48,47 @@ const getAuthHooks = (agentManager) => {
|
|
|
43
48
|
const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
44
49
|
setLoginLoading(true);
|
|
45
50
|
setLoginError(null);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
const loginPromise = new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
|
|
52
|
+
try {
|
|
53
|
+
if (!authClient) {
|
|
54
|
+
throw new Error("Auth client not initialized");
|
|
55
|
+
}
|
|
56
|
+
yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
57
|
+
? "https://identity.ic0.app/#authorize"
|
|
58
|
+
: "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
var _a;
|
|
60
|
+
const identity = yield authenticate();
|
|
61
|
+
const principal = identity.getPrincipal();
|
|
62
|
+
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
63
|
+
onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
|
|
64
|
+
resolve(principal);
|
|
65
|
+
}), onError: (e) => {
|
|
66
|
+
var _a;
|
|
67
|
+
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
68
|
+
const error = new Error("Login failed: " + e);
|
|
69
|
+
setLoginError(error);
|
|
70
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
|
|
71
|
+
reject(error);
|
|
72
|
+
} }));
|
|
50
73
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
setLoginError(e);
|
|
70
|
-
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
|
|
71
|
-
}
|
|
72
|
-
}), [authClient, onLogin, onLoginSuccess, onLoginError, isLocalEnv]);
|
|
74
|
+
catch (e) {
|
|
75
|
+
setLoginError(e);
|
|
76
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
|
|
77
|
+
reject(e);
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
setLoginLoading(false);
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
84
|
+
}), [
|
|
85
|
+
authClient,
|
|
86
|
+
onLogin,
|
|
87
|
+
onLoginSuccess,
|
|
88
|
+
onLoginError,
|
|
89
|
+
isLocalEnv,
|
|
90
|
+
authenticate,
|
|
91
|
+
]);
|
|
73
92
|
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
93
|
if (!authClient) {
|
|
75
94
|
throw new Error("Auth client not initialized");
|
package/dist/index.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options
|
|
|
16
16
|
logout: (options?: {
|
|
17
17
|
returnTo?: string | undefined;
|
|
18
18
|
} | undefined) => Promise<void>;
|
|
19
|
-
authenticate: () => Promise<
|
|
19
|
+
authenticate: () => Promise<import("@ic-reactor/store").Identity>;
|
|
20
20
|
loginLoading: boolean;
|
|
21
|
-
loginError:
|
|
21
|
+
loginError: Error | null;
|
|
22
22
|
};
|
|
23
23
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { ExtractActorMethodArgs, ExtractActorMethodReturnType, Identity, ServiceMethodType } from "@ic-reactor/store";
|
|
1
|
+
import type { ExtractActorMethodArgs, ExtractActorMethodReturnType, Identity, Principal, ServiceMethodType } from "@ic-reactor/store";
|
|
2
2
|
export type * from "@ic-reactor/store";
|
|
3
3
|
export type * from "@ic-reactor/store/dist/actor/types";
|
|
4
4
|
export type AuthArgs = {
|
|
5
|
-
onAuthentication?: () => void;
|
|
5
|
+
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
6
6
|
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
7
|
-
onAuthenticationFailure?: (error: Error
|
|
8
|
-
onLoginSuccess?: () => void;
|
|
9
|
-
onLoginError?: (error: Error
|
|
10
|
-
onLogin?: () => void;
|
|
7
|
+
onAuthenticationFailure?: (error: Error) => void;
|
|
8
|
+
onLoginSuccess?: (principal: Principal) => void;
|
|
9
|
+
onLoginError?: (error: Error) => void;
|
|
10
|
+
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
11
11
|
onLoggedOut?: () => void;
|
|
12
12
|
};
|
|
13
13
|
export type ActorCallArgs<A, M extends keyof A> = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"description": "A React library for interacting with Dfinity actors",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react": ">=16.8",
|
|
49
49
|
"zustand": "4.4"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "c28cdf54b97932fbf4b8f5fefecb36499df51eb7"
|
|
52
52
|
}
|