@auditauth/react 0.2.0-beta.2 → 0.2.0-beta.3
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/README.md +11 -0
- package/dist/guard.cjs +45 -0
- package/dist/guard.d.cts +9 -0
- package/dist/guard.d.ts +5 -2
- package/dist/guard.js +19 -22
- package/dist/index.cjs +24 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +2 -2
- package/dist/provider.cjs +131 -0
- package/dist/provider.d.cts +23 -0
- package/dist/provider.d.ts +7 -4
- package/dist/provider.js +99 -107
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -12,6 +12,17 @@ Install the package in your React application.
|
|
|
12
12
|
npm install @auditauth/react
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
## TypeScript import compatibility
|
|
16
|
+
|
|
17
|
+
`@auditauth/react` ships dual module output (ESM + CJS) with declaration files.
|
|
18
|
+
You can import it in TypeScript projects with standard syntax:
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { AuditAuthProvider } from '@auditauth/react'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
You do not need to append `.js` in consumer imports.
|
|
25
|
+
|
|
15
26
|
`@auditauth/react` has peer dependencies on `react` and `react-dom` version 18
|
|
16
27
|
or higher.
|
|
17
28
|
|
package/dist/guard.cjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var guard_exports = {};
|
|
20
|
+
__export(guard_exports, {
|
|
21
|
+
RequireAuth: () => RequireAuth
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(guard_exports);
|
|
24
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
25
|
+
var import_react = require("react");
|
|
26
|
+
var import_provider = require("./provider.js");
|
|
27
|
+
const RequireAuth = ({ children }) => {
|
|
28
|
+
const { ready, isAuthenticated, login } = (0, import_provider.useAuditAuth)();
|
|
29
|
+
const startedRef = (0, import_react.useRef)(false);
|
|
30
|
+
const authed = isAuthenticated();
|
|
31
|
+
(0, import_react.useEffect)(() => {
|
|
32
|
+
if (!ready) return;
|
|
33
|
+
if (authed) return;
|
|
34
|
+
if (startedRef.current) return;
|
|
35
|
+
startedRef.current = true;
|
|
36
|
+
login();
|
|
37
|
+
}, [ready, authed, login]);
|
|
38
|
+
if (!ready) return null;
|
|
39
|
+
if (!authed) return null;
|
|
40
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
41
|
+
};
|
|
42
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
43
|
+
0 && (module.exports = {
|
|
44
|
+
RequireAuth
|
|
45
|
+
});
|
package/dist/guard.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type RequireAuthProps = {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
};
|
|
7
|
+
declare const RequireAuth: ({ children }: RequireAuthProps) => react_jsx_runtime.JSX.Element | null;
|
|
8
|
+
|
|
9
|
+
export { RequireAuth };
|
package/dist/guard.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
2
4
|
type RequireAuthProps = {
|
|
3
5
|
children: ReactNode;
|
|
4
6
|
};
|
|
5
|
-
declare const RequireAuth: ({ children }: RequireAuthProps) =>
|
|
7
|
+
declare const RequireAuth: ({ children }: RequireAuthProps) => react_jsx_runtime.JSX.Element | null;
|
|
8
|
+
|
|
6
9
|
export { RequireAuth };
|
package/dist/guard.js
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
import { Fragment
|
|
2
|
-
import { useEffect, useRef } from
|
|
3
|
-
import { useAuditAuth } from
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useAuditAuth } from "./provider.js";
|
|
4
4
|
const RequireAuth = ({ children }) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return null;
|
|
22
|
-
return _jsx(_Fragment, { children: children });
|
|
5
|
+
const { ready, isAuthenticated, login } = useAuditAuth();
|
|
6
|
+
const startedRef = useRef(false);
|
|
7
|
+
const authed = isAuthenticated();
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (!ready) return;
|
|
10
|
+
if (authed) return;
|
|
11
|
+
if (startedRef.current) return;
|
|
12
|
+
startedRef.current = true;
|
|
13
|
+
login();
|
|
14
|
+
}, [ready, authed, login]);
|
|
15
|
+
if (!ready) return null;
|
|
16
|
+
if (!authed) return null;
|
|
17
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
18
|
+
};
|
|
19
|
+
export {
|
|
20
|
+
RequireAuth
|
|
23
21
|
};
|
|
24
|
-
export { RequireAuth };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var index_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(index_exports);
|
|
18
|
+
__reExport(index_exports, require("./provider.js"), module.exports);
|
|
19
|
+
__reExport(index_exports, require("./guard.js"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./provider.js"),
|
|
23
|
+
...require("./guard.js")
|
|
24
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { AuditAuthProvider, useAuditAuth } from './provider.js';
|
|
2
|
+
export { RequireAuth } from './guard.js';
|
|
3
|
+
import 'react/jsx-runtime';
|
|
4
|
+
import 'react';
|
|
5
|
+
import '@auditauth/core';
|
|
6
|
+
import '@auditauth/web';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
1
|
+
export * from "./provider.js";
|
|
2
|
+
export * from "./guard.js";
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var provider_exports = {};
|
|
21
|
+
__export(provider_exports, {
|
|
22
|
+
AuditAuthProvider: () => AuditAuthProvider,
|
|
23
|
+
useAuditAuth: () => useAuditAuth
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(provider_exports);
|
|
26
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
27
|
+
var import_react = require("react");
|
|
28
|
+
var import_web = require("@auditauth/web");
|
|
29
|
+
const AuditAuthContext = (0, import_react.createContext)(null);
|
|
30
|
+
const createSdk = (config) => {
|
|
31
|
+
const safeStorage = {
|
|
32
|
+
get: (name) => {
|
|
33
|
+
if (typeof window === "undefined") return null;
|
|
34
|
+
return localStorage.getItem(name);
|
|
35
|
+
},
|
|
36
|
+
set: (name, value) => {
|
|
37
|
+
if (typeof window === "undefined") return;
|
|
38
|
+
localStorage.setItem(name, value);
|
|
39
|
+
},
|
|
40
|
+
remove: (name) => {
|
|
41
|
+
if (typeof window === "undefined") return;
|
|
42
|
+
localStorage.removeItem(name);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return new import_web.AuditAuthWeb(config, safeStorage);
|
|
46
|
+
};
|
|
47
|
+
const AuditAuthProvider = ({ config, children }) => {
|
|
48
|
+
const sdkRef = (0, import_react.useRef)(null);
|
|
49
|
+
const [ready, setReady] = (0, import_react.useState)(false);
|
|
50
|
+
const [user, setUser] = (0, import_react.useState)(null);
|
|
51
|
+
if (!sdkRef.current) {
|
|
52
|
+
sdkRef.current = createSdk(config);
|
|
53
|
+
}
|
|
54
|
+
const sdk = sdkRef.current;
|
|
55
|
+
(0, import_react.useEffect)(() => {
|
|
56
|
+
let mounted = true;
|
|
57
|
+
const init = async () => {
|
|
58
|
+
try {
|
|
59
|
+
await sdk.handleRedirect();
|
|
60
|
+
const sessionUser = sdk.getSessionUser();
|
|
61
|
+
if (!mounted) return;
|
|
62
|
+
setUser(sessionUser);
|
|
63
|
+
} catch {
|
|
64
|
+
if (!mounted) return;
|
|
65
|
+
setUser(null);
|
|
66
|
+
} finally {
|
|
67
|
+
if (!mounted) return;
|
|
68
|
+
setReady(true);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
init();
|
|
72
|
+
return () => {
|
|
73
|
+
mounted = false;
|
|
74
|
+
};
|
|
75
|
+
}, [sdk]);
|
|
76
|
+
(0, import_react.useEffect)(() => {
|
|
77
|
+
if (typeof window === "undefined") return;
|
|
78
|
+
const handler = (event) => {
|
|
79
|
+
if (event.key !== "__auditauth_sync__") return;
|
|
80
|
+
const sessionUser = sdk.getSessionUser();
|
|
81
|
+
setUser(sessionUser);
|
|
82
|
+
};
|
|
83
|
+
window.addEventListener("storage", handler);
|
|
84
|
+
return () => {
|
|
85
|
+
window.removeEventListener("storage", handler);
|
|
86
|
+
};
|
|
87
|
+
}, [sdk]);
|
|
88
|
+
const logout = (0, import_react.useMemo)(() => {
|
|
89
|
+
return async () => {
|
|
90
|
+
await sdk.logout();
|
|
91
|
+
setUser(null);
|
|
92
|
+
localStorage.setItem("__auditauth_sync__", Date.now().toString());
|
|
93
|
+
};
|
|
94
|
+
}, [sdk]);
|
|
95
|
+
const fetchWrapper = (0, import_react.useMemo)(() => {
|
|
96
|
+
return async (...args) => {
|
|
97
|
+
try {
|
|
98
|
+
const res = await sdk.fetch(...args);
|
|
99
|
+
return res;
|
|
100
|
+
} catch (err) {
|
|
101
|
+
setUser(null);
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}, [sdk]);
|
|
106
|
+
const value = (0, import_react.useMemo)(() => {
|
|
107
|
+
return {
|
|
108
|
+
ready,
|
|
109
|
+
user,
|
|
110
|
+
fetch: fetchWrapper,
|
|
111
|
+
login: sdk.login.bind(sdk),
|
|
112
|
+
logout,
|
|
113
|
+
goToPortal: sdk.goToPortal.bind(sdk),
|
|
114
|
+
isAuthenticated: () => !!user,
|
|
115
|
+
trackNavigationPath: sdk.trackNavigationPath.bind(sdk)
|
|
116
|
+
};
|
|
117
|
+
}, [ready, user, sdk, logout, fetchWrapper]);
|
|
118
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuditAuthContext.Provider, { value, children });
|
|
119
|
+
};
|
|
120
|
+
const useAuditAuth = () => {
|
|
121
|
+
const ctx = (0, import_react.useContext)(AuditAuthContext);
|
|
122
|
+
if (!ctx) {
|
|
123
|
+
throw new Error("useAuditAuth must be used inside AuditAuthProvider");
|
|
124
|
+
}
|
|
125
|
+
return ctx;
|
|
126
|
+
};
|
|
127
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
+
0 && (module.exports = {
|
|
129
|
+
AuditAuthProvider,
|
|
130
|
+
useAuditAuth
|
|
131
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { AuditAuthConfig, SessionUser } from '@auditauth/core';
|
|
4
|
+
import { AuditAuthWeb } from '@auditauth/web';
|
|
5
|
+
|
|
6
|
+
type AuditAuthContextValue = {
|
|
7
|
+
ready: boolean;
|
|
8
|
+
user: SessionUser | null;
|
|
9
|
+
fetch: AuditAuthWeb['fetch'];
|
|
10
|
+
login: () => Promise<void>;
|
|
11
|
+
logout: () => Promise<void>;
|
|
12
|
+
goToPortal: () => Promise<void>;
|
|
13
|
+
isAuthenticated: () => boolean;
|
|
14
|
+
trackNavigationPath: (path: string) => void;
|
|
15
|
+
};
|
|
16
|
+
type AuditAuthProviderProps = {
|
|
17
|
+
config: AuditAuthConfig;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
};
|
|
20
|
+
declare const AuditAuthProvider: ({ config, children }: AuditAuthProviderProps) => react_jsx_runtime.JSX.Element;
|
|
21
|
+
declare const useAuditAuth: () => AuditAuthContextValue;
|
|
22
|
+
|
|
23
|
+
export { AuditAuthProvider, useAuditAuth };
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
import { AuditAuthConfig, SessionUser } from '@auditauth/core';
|
|
3
4
|
import { AuditAuthWeb } from '@auditauth/web';
|
|
5
|
+
|
|
4
6
|
type AuditAuthContextValue = {
|
|
5
7
|
ready: boolean;
|
|
6
8
|
user: SessionUser | null;
|
|
@@ -15,6 +17,7 @@ type AuditAuthProviderProps = {
|
|
|
15
17
|
config: AuditAuthConfig;
|
|
16
18
|
children: ReactNode;
|
|
17
19
|
};
|
|
18
|
-
declare const AuditAuthProvider: ({ config, children }: AuditAuthProviderProps) =>
|
|
20
|
+
declare const AuditAuthProvider: ({ config, children }: AuditAuthProviderProps) => react_jsx_runtime.JSX.Element;
|
|
19
21
|
declare const useAuditAuth: () => AuditAuthContextValue;
|
|
20
|
-
|
|
22
|
+
|
|
23
|
+
export { AuditAuthProvider, useAuditAuth };
|
package/dist/provider.js
CHANGED
|
@@ -1,114 +1,106 @@
|
|
|
1
|
-
|
|
2
|
-
import { jsx
|
|
3
|
-
import { createContext, useContext, useEffect, useMemo, useRef, useState } from
|
|
4
|
-
import { AuditAuthWeb } from
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { AuditAuthWeb } from "@auditauth/web";
|
|
5
5
|
const AuditAuthContext = createContext(null);
|
|
6
6
|
const createSdk = (config) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
return new AuditAuthWeb(config, safeStorage);
|
|
7
|
+
const safeStorage = {
|
|
8
|
+
get: (name) => {
|
|
9
|
+
if (typeof window === "undefined") return null;
|
|
10
|
+
return localStorage.getItem(name);
|
|
11
|
+
},
|
|
12
|
+
set: (name, value) => {
|
|
13
|
+
if (typeof window === "undefined") return;
|
|
14
|
+
localStorage.setItem(name, value);
|
|
15
|
+
},
|
|
16
|
+
remove: (name) => {
|
|
17
|
+
if (typeof window === "undefined") return;
|
|
18
|
+
localStorage.removeItem(name);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
return new AuditAuthWeb(config, safeStorage);
|
|
25
22
|
};
|
|
26
23
|
const AuditAuthProvider = ({ config, children }) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
login: sdk.login.bind(sdk),
|
|
99
|
-
logout,
|
|
100
|
-
goToPortal: sdk.goToPortal.bind(sdk),
|
|
101
|
-
isAuthenticated: () => !!user,
|
|
102
|
-
trackNavigationPath: sdk.trackNavigationPath.bind(sdk),
|
|
103
|
-
};
|
|
104
|
-
}, [ready, user, sdk, logout, fetchWrapper]);
|
|
105
|
-
return (_jsx(AuditAuthContext.Provider, { value: value, children: children }));
|
|
24
|
+
const sdkRef = useRef(null);
|
|
25
|
+
const [ready, setReady] = useState(false);
|
|
26
|
+
const [user, setUser] = useState(null);
|
|
27
|
+
if (!sdkRef.current) {
|
|
28
|
+
sdkRef.current = createSdk(config);
|
|
29
|
+
}
|
|
30
|
+
const sdk = sdkRef.current;
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
let mounted = true;
|
|
33
|
+
const init = async () => {
|
|
34
|
+
try {
|
|
35
|
+
await sdk.handleRedirect();
|
|
36
|
+
const sessionUser = sdk.getSessionUser();
|
|
37
|
+
if (!mounted) return;
|
|
38
|
+
setUser(sessionUser);
|
|
39
|
+
} catch {
|
|
40
|
+
if (!mounted) return;
|
|
41
|
+
setUser(null);
|
|
42
|
+
} finally {
|
|
43
|
+
if (!mounted) return;
|
|
44
|
+
setReady(true);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
init();
|
|
48
|
+
return () => {
|
|
49
|
+
mounted = false;
|
|
50
|
+
};
|
|
51
|
+
}, [sdk]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (typeof window === "undefined") return;
|
|
54
|
+
const handler = (event) => {
|
|
55
|
+
if (event.key !== "__auditauth_sync__") return;
|
|
56
|
+
const sessionUser = sdk.getSessionUser();
|
|
57
|
+
setUser(sessionUser);
|
|
58
|
+
};
|
|
59
|
+
window.addEventListener("storage", handler);
|
|
60
|
+
return () => {
|
|
61
|
+
window.removeEventListener("storage", handler);
|
|
62
|
+
};
|
|
63
|
+
}, [sdk]);
|
|
64
|
+
const logout = useMemo(() => {
|
|
65
|
+
return async () => {
|
|
66
|
+
await sdk.logout();
|
|
67
|
+
setUser(null);
|
|
68
|
+
localStorage.setItem("__auditauth_sync__", Date.now().toString());
|
|
69
|
+
};
|
|
70
|
+
}, [sdk]);
|
|
71
|
+
const fetchWrapper = useMemo(() => {
|
|
72
|
+
return async (...args) => {
|
|
73
|
+
try {
|
|
74
|
+
const res = await sdk.fetch(...args);
|
|
75
|
+
return res;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
setUser(null);
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}, [sdk]);
|
|
82
|
+
const value = useMemo(() => {
|
|
83
|
+
return {
|
|
84
|
+
ready,
|
|
85
|
+
user,
|
|
86
|
+
fetch: fetchWrapper,
|
|
87
|
+
login: sdk.login.bind(sdk),
|
|
88
|
+
logout,
|
|
89
|
+
goToPortal: sdk.goToPortal.bind(sdk),
|
|
90
|
+
isAuthenticated: () => !!user,
|
|
91
|
+
trackNavigationPath: sdk.trackNavigationPath.bind(sdk)
|
|
92
|
+
};
|
|
93
|
+
}, [ready, user, sdk, logout, fetchWrapper]);
|
|
94
|
+
return /* @__PURE__ */ jsx(AuditAuthContext.Provider, { value, children });
|
|
106
95
|
};
|
|
107
96
|
const useAuditAuth = () => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
97
|
+
const ctx = useContext(AuditAuthContext);
|
|
98
|
+
if (!ctx) {
|
|
99
|
+
throw new Error("useAuditAuth must be used inside AuditAuthProvider");
|
|
100
|
+
}
|
|
101
|
+
return ctx;
|
|
102
|
+
};
|
|
103
|
+
export {
|
|
104
|
+
AuditAuthProvider,
|
|
105
|
+
useAuditAuth
|
|
113
106
|
};
|
|
114
|
-
export { AuditAuthProvider, useAuditAuth, };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auditauth/react",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.3",
|
|
4
4
|
"description": "AuditAuth React SDK",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Nimibyte",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"security",
|
|
25
25
|
"auditauth"
|
|
26
26
|
],
|
|
27
|
-
"module": "dist/index.js",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
28
|
"type": "module",
|
|
29
|
-
"main": "dist/index.
|
|
30
|
-
"types": "dist/index.d.ts",
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
31
|
"files": [
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
@@ -35,21 +35,24 @@
|
|
|
35
35
|
"exports": {
|
|
36
36
|
".": {
|
|
37
37
|
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js",
|
|
39
|
+
"require": "./dist/index.cjs",
|
|
38
40
|
"default": "./dist/index.js"
|
|
39
41
|
},
|
|
40
42
|
"./package.json": "./package.json"
|
|
41
43
|
},
|
|
42
44
|
"scripts": {
|
|
43
|
-
"build": "
|
|
44
|
-
"dev": "
|
|
45
|
-
"clean": "rm -rf dist"
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"dev": "tsup --watch",
|
|
47
|
+
"clean": "rm -rf dist",
|
|
48
|
+
"prepack": "npm run build"
|
|
46
49
|
},
|
|
47
50
|
"peerDependencies": {
|
|
48
51
|
"react": ">=18",
|
|
49
52
|
"react-dom": ">=18"
|
|
50
53
|
},
|
|
51
54
|
"dependencies": {
|
|
52
|
-
"@auditauth/web": "^0.2.0-beta.
|
|
55
|
+
"@auditauth/web": "^0.2.0-beta.3"
|
|
53
56
|
},
|
|
54
57
|
"devDependencies": {
|
|
55
58
|
"@types/react": "^18.2.0",
|