@ic-reactor/react 0.4.2 → 0.4.4

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