@echoxyz/sonar-react 0.1.4 → 0.2.0

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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # @echoxyz/sonar-react
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 77db07a: Change authenticated to bool, add ready state
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [77db07a]
12
+ - @echoxyz/sonar-core@0.1.5
13
+
3
14
  ## 0.1.4
4
15
 
5
16
  ### Patch Changes
package/README.md CHANGED
@@ -45,8 +45,8 @@ export function AppRoot({ children }: { children: React.ReactNode }) {
45
45
  import { useSonarAuth } from "@echoxyz/sonar-react";
46
46
 
47
47
  export function LoginButton() {
48
- const { login, authenticated } = useSonarAuth();
49
- if (authenticated()) {
48
+ const { login, authenticated, ready } = useSonarAuth();
49
+ if (!ready || authenticated) {
50
50
  return null;
51
51
  }
52
52
  return <button onClick={() => login()}>Sign in with Echo</button>;
@@ -86,11 +86,11 @@ import { useSonarAuth, useSonarClient } from "@echoxyz/sonar-react";
86
86
  import { EntityType } from "@echoxyz/sonar-core";
87
87
 
88
88
  export function Example() {
89
- const { authenticated } = useSonarAuth();
89
+ const { authenticated, ready } = useSonarAuth();
90
90
  const client = useSonarClient();
91
91
 
92
92
  useEffect(() => {
93
- if (!authenticated()) {
93
+ if (!ready || !authenticated) {
94
94
  return;
95
95
  }
96
96
 
@@ -122,7 +122,7 @@ export function Example() {
122
122
  });
123
123
  console.log(alloc);
124
124
  })();
125
- }, [authenticated, client]);
125
+ }, [authenticated, client, ready]);
126
126
 
127
127
  return null;
128
128
  }
@@ -138,7 +138,7 @@ export function Example() {
138
138
  - `apiURL?: string` (default: `https://api.echo.xyz`) – API base URL.
139
139
  - `tokenStorageKey?: string` (default: `sonar:auth-token`) – Browser storage key for the access token.
140
140
 
141
- - `useSonarAuth()` → `{ authenticated, token?, login(), completeOAuth({ code, state }), logout() }`
141
+ - `useSonarAuth()` → `{ authenticated, ready, token?, login(), completeOAuth({ code, state }), logout() }`
142
142
 
143
143
  - `useSonarClient()` → low-level `SonarClient` instance.
144
144
 
package/dist/index.cjs CHANGED
@@ -33,13 +33,20 @@ var import_jsx_runtime = require("react/jsx-runtime");
33
33
  var AuthContext = (0, import_react.createContext)(void 0);
34
34
  var ClientContext = (0, import_react.createContext)(void 0);
35
35
  function SonarProvider({ children, config }) {
36
+ const [authState, setAuthState] = (0, import_react.useState)({ token: void 0, ready: false });
36
37
  const client = (0, import_react.useMemo)(() => {
37
38
  return (0, import_sonar_core.createClient)({
38
39
  saleUUID: config.saleUUID,
39
40
  apiURL: config.apiURL,
40
- tokenKey: config.tokenStorageKey
41
+ tokenKey: config.tokenStorageKey,
42
+ onTokenChange: (nextToken) => {
43
+ setAuthState({ token: nextToken ?? void 0, ready: true });
44
+ }
41
45
  });
42
46
  }, [config.apiURL, config.saleUUID, config.tokenStorageKey]);
47
+ (0, import_react.useEffect)(() => {
48
+ setAuthState({ token: client.getToken() ?? void 0, ready: true });
49
+ }, [client]);
43
50
  const login = (0, import_react.useCallback)(async () => {
44
51
  const { codeVerifier, codeChallenge, state } = await (0, import_sonar_core.generatePKCEParams)();
45
52
  if (typeof window === "undefined") {
@@ -80,16 +87,17 @@ function SonarProvider({ children, config }) {
80
87
  );
81
88
  const logout = (0, import_react.useCallback)(() => {
82
89
  client.clear();
83
- }, []);
90
+ }, [client]);
84
91
  const authValue = (0, import_react.useMemo)(
85
92
  () => ({
86
93
  login,
87
94
  logout,
88
- authenticated: () => Boolean(client.getToken()),
89
- token: client.getToken() ?? void 0,
95
+ authenticated: Boolean(authState.token),
96
+ ready: authState.ready,
97
+ token: authState.token,
90
98
  completeOAuth
91
99
  }),
92
- [client, login, completeOAuth, logout]
100
+ [authState, login, completeOAuth, logout]
93
101
  );
94
102
  const clientValue = (0, import_react.useMemo)(() => ({ client }), [client]);
95
103
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: authValue, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ClientContext.Provider, { value: clientValue, children }) });
package/dist/index.d.cts CHANGED
@@ -15,7 +15,8 @@ type SonarProviderConfig = {
15
15
  tokenStorageKey?: string;
16
16
  };
17
17
  type AuthContextValue = {
18
- authenticated: () => boolean;
18
+ authenticated: boolean;
19
+ ready: boolean;
19
20
  token?: string;
20
21
  login: () => Promise<void>;
21
22
  completeOAuth: (args: {
package/dist/index.d.ts CHANGED
@@ -15,7 +15,8 @@ type SonarProviderConfig = {
15
15
  tokenStorageKey?: string;
16
16
  };
17
17
  type AuthContextValue = {
18
- authenticated: () => boolean;
18
+ authenticated: boolean;
19
+ ready: boolean;
19
20
  token?: string;
20
21
  login: () => Promise<void>;
21
22
  completeOAuth: (args: {
package/dist/index.js CHANGED
@@ -1,17 +1,24 @@
1
1
  // src/react.tsx
2
2
  import { buildAuthorizationUrl, createClient, generatePKCEParams } from "@echoxyz/sonar-core";
3
- import { createContext, useCallback, useContext, useMemo } from "react";
3
+ import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
  var AuthContext = createContext(void 0);
6
6
  var ClientContext = createContext(void 0);
7
7
  function SonarProvider({ children, config }) {
8
+ const [authState, setAuthState] = useState({ token: void 0, ready: false });
8
9
  const client = useMemo(() => {
9
10
  return createClient({
10
11
  saleUUID: config.saleUUID,
11
12
  apiURL: config.apiURL,
12
- tokenKey: config.tokenStorageKey
13
+ tokenKey: config.tokenStorageKey,
14
+ onTokenChange: (nextToken) => {
15
+ setAuthState({ token: nextToken ?? void 0, ready: true });
16
+ }
13
17
  });
14
18
  }, [config.apiURL, config.saleUUID, config.tokenStorageKey]);
19
+ useEffect(() => {
20
+ setAuthState({ token: client.getToken() ?? void 0, ready: true });
21
+ }, [client]);
15
22
  const login = useCallback(async () => {
16
23
  const { codeVerifier, codeChallenge, state } = await generatePKCEParams();
17
24
  if (typeof window === "undefined") {
@@ -52,16 +59,17 @@ function SonarProvider({ children, config }) {
52
59
  );
53
60
  const logout = useCallback(() => {
54
61
  client.clear();
55
- }, []);
62
+ }, [client]);
56
63
  const authValue = useMemo(
57
64
  () => ({
58
65
  login,
59
66
  logout,
60
- authenticated: () => Boolean(client.getToken()),
61
- token: client.getToken() ?? void 0,
67
+ authenticated: Boolean(authState.token),
68
+ ready: authState.ready,
69
+ token: authState.token,
62
70
  completeOAuth
63
71
  }),
64
- [client, login, completeOAuth, logout]
72
+ [authState, login, completeOAuth, logout]
65
73
  );
66
74
  const clientValue = useMemo(() => ({ client }), [client]);
67
75
  return /* @__PURE__ */ jsx(AuthContext.Provider, { value: authValue, children: /* @__PURE__ */ jsx(ClientContext.Provider, { value: clientValue, children }) });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@echoxyz/sonar-react",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -16,12 +16,17 @@
16
16
  "react": ">=18"
17
17
  },
18
18
  "dependencies": {
19
- "@echoxyz/sonar-core": "0.1.4"
19
+ "@echoxyz/sonar-core": "0.1.5"
20
20
  },
21
21
  "devDependencies": {
22
+ "@testing-library/react": "^16.0.0",
23
+ "@types/react": "^18",
24
+ "@types/react-dom": "^18",
25
+ "jsdom": "^27.0.0",
26
+ "react": "^18",
27
+ "react-dom": "^18",
22
28
  "tsup": "^8.0.0",
23
- "vitest": "^2.0.0",
24
- "@types/react": "^18"
29
+ "vitest": "^2.0.0"
25
30
  },
26
31
  "repository": {
27
32
  "type": "git",