@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.
@@ -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: import("@ic-reactor/store").Identity | null;
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<void>;
17
+ authenticate: () => Promise<Identity>;
18
18
  loginLoading: boolean;
19
- loginError: unknown;
19
+ loginError: Error | null;
20
20
  };
21
21
  };
@@ -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
- 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
- });
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
- onLogin === null || onLogin === void 0 ? void 0 : onLogin();
47
- try {
48
- if (!authClient) {
49
- throw new Error("Auth client not initialized");
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
- 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
- setLoginLoading(false);
56
- yield authenticate();
57
- (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
58
- onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess();
59
- }), onError: (e) => {
60
- var _a;
61
- setLoginError(e);
62
- setLoginLoading(false);
63
- (_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
64
- onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
65
- } }));
66
- }
67
- catch (e) {
68
- setLoginLoading(false);
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<void>;
19
+ authenticate: () => Promise<import("@ic-reactor/store").Identity>;
20
20
  loginLoading: boolean;
21
- loginError: unknown;
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 | unknown | null) => void;
8
- onLoginSuccess?: () => void;
9
- onLoginError?: (error: Error | unknown | null) => 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.3",
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": "b7de729b76ac8e1db098bf2eadf975cfb4a57fa2"
51
+ "gitHead": "c28cdf54b97932fbf4b8f5fefecb36499df51eb7"
52
52
  }