@ic-reactor/react 0.4.4 → 0.4.6

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,20 +1,21 @@
1
1
  import type { AuthClientLoginOptions } from "@dfinity/auth-client";
2
- import type { AgentManager } from "@ic-reactor/store";
2
+ import type { AgentManager, Identity, Principal } 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) => {
6
+ useUserPrincipal: () => Principal | undefined;
6
7
  useAgentManager: () => AgentManager;
7
8
  useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
8
9
  useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: AuthArgs) => {
9
10
  authClient: import("@dfinity/auth-client").AuthClient | null;
10
11
  authenticated: boolean;
11
12
  authenticating: boolean;
12
- identity: import("@ic-reactor/store").Identity | null;
13
+ identity: Identity | null;
13
14
  login: (options?: AuthClientLoginOptions) => Promise<void>;
14
15
  logout: (options?: {
15
16
  returnTo?: string;
16
17
  }) => Promise<void>;
17
- authenticate: () => Promise<void>;
18
+ authenticate: () => Promise<Identity>;
18
19
  loginLoading: boolean;
19
20
  loginError: Error | null;
20
21
  };
@@ -21,19 +21,28 @@ const getAuthHooks = (agentManager) => {
21
21
  const authState = (0, zustand_1.useStore)(authStore, (state) => state);
22
22
  return authState;
23
23
  };
24
+ const useUserPrincipal = () => {
25
+ const { identity } = useAuthStore();
26
+ return identity === null || identity === void 0 ? void 0 : identity.getPrincipal();
27
+ };
24
28
  const useAuthClient = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
25
29
  const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
26
30
  const [loginError, setLoginError] = (0, react_1.useState)(null);
27
31
  const { authClient, authenticated, authenticating, identity } = useAuthStore();
28
32
  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
- });
33
+ const authenticatePromise = new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
34
+ try {
35
+ const identity = yield authenticator();
36
+ onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
37
+ resolve(identity);
38
+ }
39
+ catch (e) {
40
+ onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
41
+ reject(e);
42
+ }
43
+ }));
44
+ onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
45
+ return authenticatePromise;
37
46
  }), [
38
47
  authenticator,
39
48
  onAuthentication,
@@ -43,34 +52,47 @@ const getAuthHooks = (agentManager) => {
43
52
  const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
44
53
  setLoginLoading(true);
45
54
  setLoginError(null);
46
- onLogin === null || onLogin === void 0 ? void 0 : onLogin();
47
- try {
48
- if (!authClient) {
49
- throw new Error("Auth client not initialized");
55
+ const loginPromise = new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
56
+ try {
57
+ if (!authClient) {
58
+ throw new Error("Auth client not initialized");
59
+ }
60
+ yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
61
+ ? "https://identity.ic0.app/#authorize"
62
+ : "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
63
+ var _a;
64
+ const identity = yield authenticate();
65
+ const principal = identity.getPrincipal();
66
+ (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
67
+ onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
68
+ resolve(principal);
69
+ }), onError: (e) => {
70
+ var _a;
71
+ (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
72
+ const error = new Error("Login failed: " + e);
73
+ setLoginError(error);
74
+ onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
75
+ reject(error);
76
+ } }));
50
77
  }
51
- return yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
52
- ? "https://identity.ic0.app/#authorize"
53
- : "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
54
- var _a;
55
- yield authenticate();
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();
58
- }), onError: (e) => {
59
- var _a;
60
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
61
- const error = new Error("Login failed:" + e);
62
- setLoginError(error);
63
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(error);
64
- } }));
65
- }
66
- catch (e) {
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]);
78
+ catch (e) {
79
+ setLoginError(e);
80
+ onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
81
+ reject(e);
82
+ }
83
+ finally {
84
+ setLoginLoading(false);
85
+ }
86
+ }));
87
+ onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
88
+ }), [
89
+ authClient,
90
+ onLogin,
91
+ onLoginSuccess,
92
+ onLoginError,
93
+ isLocalEnv,
94
+ authenticate,
95
+ ]);
74
96
  const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
75
97
  if (!authClient) {
76
98
  throw new Error("Auth client not initialized");
@@ -97,6 +119,7 @@ const getAuthHooks = (agentManager) => {
97
119
  };
98
120
  };
99
121
  return {
122
+ useUserPrincipal,
100
123
  useAgentManager,
101
124
  useAuthStore,
102
125
  useAuthClient,
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { createReActorStore, createAgentManager, createActorManager, } from "@ic
5
5
  export * from "./context/agent";
6
6
  export * from "./context/actor";
7
7
  export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options }: CreateReActorOptions) => ActorHooks<A> & {
8
+ useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
8
9
  useAgentManager: () => import("@ic-reactor/store").AgentManager;
9
10
  useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
10
11
  useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: import("./types").AuthArgs) => {
@@ -16,7 +17,7 @@ export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options
16
17
  logout: (options?: {
17
18
  returnTo?: string | undefined;
18
19
  } | undefined) => Promise<void>;
19
- authenticate: () => Promise<void>;
20
+ authenticate: () => Promise<import("@ic-reactor/store").Identity>;
20
21
  loginLoading: boolean;
21
22
  loginError: Error | null;
22
23
  };
package/dist/index.js CHANGED
@@ -43,10 +43,11 @@ const createReActor = (_a) => {
43
43
  (process.env.NODE_ENV === "development" ||
44
44
  process.env.DFX_NETWORK === "local"));
45
45
  const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv }, options));
46
- const { useAuthClient, useAgentManager, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
46
+ const { useAuthClient, useUserPrincipal, useAgentManager, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
47
47
  const { initialize, useActorStore, useQueryCall, useUpdateCall, useMethodCall, useMethodField, useMethodFields, useMethodNames, useServiceFields, } = (0, actor_1.getActorHooks)(actorManager);
48
48
  return {
49
49
  initialize,
50
+ useUserPrincipal,
50
51
  useAgentManager,
51
52
  useMethodFields,
52
53
  useMethodField,
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 | undefined) => void;
8
- onLoginSuccess?: () => void;
9
- onLoginError?: (error: Error | undefined) => void;
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.4",
3
+ "version": "0.4.6",
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": "be65990c31a4e7a86539fec2c6600de3e32a0c79"
51
+ "gitHead": "48251ebc25add07c5a94ff63ebc8bf2dc8e0caba"
52
52
  }