@oxyhq/services 0.0.71 → 0.0.72

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.
@@ -12,8 +12,6 @@ interface SessionModel {
12
12
  role: UserRole;
13
13
  isOAuth: boolean;
14
14
  isTwoFactorEnabled: boolean;
15
- lastLogin: string;
16
- permissions: string[];
17
15
  };
18
16
  }
19
17
  type SessionState = {
@@ -1 +1 @@
1
- {"version":3,"file":"useOxySession.d.ts","sourceRoot":"","sources":["../../src/hooks/useOxySession.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,UAAU,YAAY;IACpB,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAC;QACX,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,OAAO,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,QAAQ,CAAC;QACf,OAAO,EAAE,OAAO,CAAC;QACjB,kBAAkB,EAAE,OAAO,CAAC;QAC5B,SAAS,EAAE,MAAM,CAAC;QAClB,WAAW,EAAE,MAAM,EAAE,CAAC;KACvB,CAAC;CACH;AAED,KAAK,YAAY,GAAG;IAClB,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,GAAG,CAAC;IACX,gBAAgB,EAAE,MAAM,IAAI,CAAC;CAC9B,CAAC;AAEF,eAAO,MAAM,eAAe,2EAiDzB,CAAC;AAEJ,iBAAS,aAAa;;;;EAQrB;AAED,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"useOxySession.d.ts","sourceRoot":"","sources":["../../src/hooks/useOxySession.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,UAAU,YAAY;IACpB,IAAI,EAAE;QACJ,EAAE,EAAE,MAAM,CAAC;QACX,QAAQ,EAAE,MAAM,CAAC;QACjB,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,MAAM,CAAC;QACjB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE,OAAO,CAAC;QAClB,MAAM,EAAE,MAAM,CAAC;QACf,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,QAAQ,CAAC;QACf,OAAO,EAAE,OAAO,CAAC;QACjB,kBAAkB,EAAE,OAAO,CAAC;KAC7B,CAAC;CACH;AAED,KAAK,YAAY,GAAG;IAClB,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,GAAG,CAAC;IACX,gBAAgB,EAAE,MAAM,IAAI,CAAC;CAC9B,CAAC;AAEF,eAAO,MAAM,eAAe,2EA4D1B,CAAC;AAEH,iBAAS,aAAa;;;;EAQrB;AAED,eAAe,aAAa,CAAC"}
@@ -3,58 +3,71 @@ import axios from "axios";
3
3
  import { create } from "zustand";
4
4
  import localforage from "localforage";
5
5
  import { OXY_AUTH_URL } from "../config";
6
- export const useSessionStore = create((set) => ({
7
- session: null,
8
- status: "idle",
9
- error: null,
10
- fetchSessionData: async () => {
11
- set({ status: "loading" });
12
- try {
13
- let clientKey = null;
14
- if (typeof window !== "undefined") {
15
- const urlParams = new URLSearchParams(window.location.search);
16
- clientKey = urlParams.get("clientKey");
17
- }
18
- if (!clientKey) {
19
- clientKey = await localforage.getItem("clientKey");
20
- }
21
- else {
22
- await localforage.setItem("clientKey", clientKey);
23
- }
24
- if (!clientKey) {
25
- throw new Error("Client key not found.");
26
- }
27
- const response = await axios.get(`${OXY_AUTH_URL}/api/session/${clientKey}`);
28
- if (response.status !== 200) {
29
- throw new Error(`Unexpected response status: ${response.status}`);
30
- }
31
- set({ session: response.data, status: "success", error: null });
32
- }
33
- catch (error) {
34
- let errorMessage = "An error occurred while loading the session data.";
35
- if (axios.isAxiosError(error)) {
36
- if (error.response) {
37
- errorMessage = `API error: ${error.response.status} ${error.response.statusText}`;
6
+ export const useSessionStore = create((set) => {
7
+ let isFirstFetch = true;
8
+ return {
9
+ session: null,
10
+ status: "loading",
11
+ error: null,
12
+ fetchSessionData: async () => {
13
+ try {
14
+ if (isFirstFetch) {
15
+ set({ status: "loading" });
16
+ isFirstFetch = false;
38
17
  }
39
- else if (error.request) {
40
- errorMessage = "Network error: No response received from server.";
18
+ // Get the session ID from the URL parameters if it exists
19
+ const urlParams = new URLSearchParams(window.location.search);
20
+ let clientKey = urlParams.get("clientKey");
21
+ // If the session ID was not found in the URL parameters, get it from local storage
22
+ if (!clientKey) {
23
+ clientKey = await localforage.getItem("clientKey");
41
24
  }
42
25
  else {
43
- errorMessage = `Axios error: ${error.message}`;
26
+ // If the session ID was found in the URL parameters, set it in local storage
27
+ await localforage.setItem("clientKey", clientKey);
28
+ }
29
+ const response = await axios.get(OXY_AUTH_URL + "/api/session/" + clientKey);
30
+ if (response.status !== 200) {
31
+ throw new Error(`Unexpected response status: ${response.status}`);
44
32
  }
33
+ set({ session: response.data, status: "success" });
45
34
  }
46
- else if (error instanceof Error) {
47
- errorMessage = `Error: ${error.message}`;
35
+ catch (error) {
36
+ if (axios.isAxiosError(error)) {
37
+ if (error.response) {
38
+ set({
39
+ error: `Network error: ${error.message}, Status code: ${error.response.status}`,
40
+ status: "error",
41
+ });
42
+ }
43
+ else if (error.request) {
44
+ set({
45
+ error: "Network error: No response received from server.",
46
+ status: "error",
47
+ });
48
+ }
49
+ else {
50
+ set({
51
+ error: `Network error: ${error.message}`,
52
+ status: "error",
53
+ });
54
+ }
55
+ }
56
+ else {
57
+ set({
58
+ error: "An unknown error occurred while loading the session data.",
59
+ status: "error",
60
+ });
61
+ }
48
62
  }
49
- set({ error: errorMessage, status: "error", session: null });
50
- }
51
- },
52
- }));
63
+ },
64
+ };
65
+ });
53
66
  function useOxySession() {
54
67
  const { session, status, error, fetchSessionData } = useSessionStore();
55
68
  useEffect(() => {
56
69
  fetchSessionData();
57
- }, [fetchSessionData]);
70
+ }, []);
58
71
  return { session, status, error };
59
72
  }
60
73
  export default useOxySession;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxyhq/services",
3
- "version": "0.0.71",
3
+ "version": "0.0.72",
4
4
  "description": "",
5
5
  "homepage": "https://oxy.so/",
6
6
  "main": "./dist/index.js",