nextauthz 1.0.7 → 1.0.9
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/dist/index.d.mts +2 -24
- package/dist/index.d.ts +2 -24
- package/dist/index.js +6 -102
- package/dist/index.mjs +6 -99
- package/package.json +1 -1
- package/src/AuthGuard.tsx +56 -56
- package/src/RoleGuard.tsx +40 -39
- package/src/index.ts +7 -7
- package/src/myAuth.ts +4 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
3
|
-
import React__default, { ReactNode } from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
4
3
|
|
|
5
4
|
type AuthContextType<UserType> = {
|
|
6
5
|
user: UserType | null;
|
|
@@ -23,29 +22,8 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
|
23
22
|
useAuth: () => AuthContextType<UserType>;
|
|
24
23
|
};
|
|
25
24
|
|
|
26
|
-
type AuthGuardProps = {
|
|
27
|
-
children: React__default.ReactNode;
|
|
28
|
-
redirectTo?: string;
|
|
29
|
-
tokenKey?: string;
|
|
30
|
-
refreshToken?: () => Promise<string | null>;
|
|
31
|
-
};
|
|
32
|
-
declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
33
|
-
|
|
34
|
-
type RoleGuardProps = {
|
|
35
|
-
children: React__default.ReactNode;
|
|
36
|
-
allowedRoles: string[];
|
|
37
|
-
redirectTo?: string;
|
|
38
|
-
};
|
|
39
|
-
declare const RoleGuard: ({ children, allowedRoles, redirectTo, }: RoleGuardProps) => react_jsx_runtime.JSX.Element;
|
|
40
|
-
|
|
41
25
|
type User = {
|
|
42
26
|
[key: string]: any;
|
|
43
27
|
};
|
|
44
|
-
declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
|
|
45
|
-
AuthProvider: ({ children }: {
|
|
46
|
-
children: React.ReactNode;
|
|
47
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
48
|
-
useAuth: () => AuthContextType<User>;
|
|
49
|
-
};
|
|
50
28
|
|
|
51
|
-
export {
|
|
29
|
+
export { type User, createAuthContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
3
|
-
import React__default, { ReactNode } from 'react';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
4
3
|
|
|
5
4
|
type AuthContextType<UserType> = {
|
|
6
5
|
user: UserType | null;
|
|
@@ -23,29 +22,8 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
|
23
22
|
useAuth: () => AuthContextType<UserType>;
|
|
24
23
|
};
|
|
25
24
|
|
|
26
|
-
type AuthGuardProps = {
|
|
27
|
-
children: React__default.ReactNode;
|
|
28
|
-
redirectTo?: string;
|
|
29
|
-
tokenKey?: string;
|
|
30
|
-
refreshToken?: () => Promise<string | null>;
|
|
31
|
-
};
|
|
32
|
-
declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
33
|
-
|
|
34
|
-
type RoleGuardProps = {
|
|
35
|
-
children: React__default.ReactNode;
|
|
36
|
-
allowedRoles: string[];
|
|
37
|
-
redirectTo?: string;
|
|
38
|
-
};
|
|
39
|
-
declare const RoleGuard: ({ children, allowedRoles, redirectTo, }: RoleGuardProps) => react_jsx_runtime.JSX.Element;
|
|
40
|
-
|
|
41
25
|
type User = {
|
|
42
26
|
[key: string]: any;
|
|
43
27
|
};
|
|
44
|
-
declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
|
|
45
|
-
AuthProvider: ({ children }: {
|
|
46
|
-
children: React.ReactNode;
|
|
47
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
48
|
-
useAuth: () => AuthContextType<User>;
|
|
49
|
-
};
|
|
50
28
|
|
|
51
|
-
export {
|
|
29
|
+
export { type User, createAuthContext };
|
package/dist/index.js
CHANGED
|
@@ -1699,18 +1699,18 @@ var require_react_jsx_runtime_development = __commonJS({
|
|
|
1699
1699
|
function isValidElement(object) {
|
|
1700
1700
|
return "object" === typeof object && null !== object && object.$$typeof === REACT_ELEMENT_TYPE;
|
|
1701
1701
|
}
|
|
1702
|
-
var
|
|
1702
|
+
var React2 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React2.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
|
|
1703
1703
|
return null;
|
|
1704
1704
|
};
|
|
1705
|
-
|
|
1705
|
+
React2 = {
|
|
1706
1706
|
react_stack_bottom_frame: function(callStackForError) {
|
|
1707
1707
|
return callStackForError();
|
|
1708
1708
|
}
|
|
1709
1709
|
};
|
|
1710
1710
|
var specialPropKeyWarningShown;
|
|
1711
1711
|
var didWarnAboutElementRef = {};
|
|
1712
|
-
var unknownOwnerDebugStack =
|
|
1713
|
-
|
|
1712
|
+
var unknownOwnerDebugStack = React2.react_stack_bottom_frame.bind(
|
|
1713
|
+
React2,
|
|
1714
1714
|
UnknownOwner
|
|
1715
1715
|
)();
|
|
1716
1716
|
var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
|
|
@@ -1757,9 +1757,6 @@ var require_jsx_runtime = __commonJS({
|
|
|
1757
1757
|
// src/index.ts
|
|
1758
1758
|
var index_exports = {};
|
|
1759
1759
|
__export(index_exports, {
|
|
1760
|
-
AuthGuard: () => AuthGuard_default,
|
|
1761
|
-
RoleGuard: () => RoleGuard_default,
|
|
1762
|
-
createAppAuth: () => createAppAuth,
|
|
1763
1760
|
createAuthContext: () => createAuthContext
|
|
1764
1761
|
});
|
|
1765
1762
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -1850,108 +1847,15 @@ function createAuthContext(options) {
|
|
|
1850
1847
|
}
|
|
1851
1848
|
);
|
|
1852
1849
|
};
|
|
1853
|
-
const
|
|
1850
|
+
const useAuth = () => {
|
|
1854
1851
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
1855
1852
|
if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
|
|
1856
1853
|
return ctx;
|
|
1857
1854
|
};
|
|
1858
|
-
return { AuthProvider, useAuth
|
|
1859
|
-
}
|
|
1860
|
-
|
|
1861
|
-
// src/AuthGuard.tsx
|
|
1862
|
-
var import_react2 = __toESM(require_react());
|
|
1863
|
-
var import_navigation = require("next/navigation");
|
|
1864
|
-
var import_react_token_manager2 = require("react-token-manager");
|
|
1865
|
-
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1866
|
-
var AuthGuard = ({
|
|
1867
|
-
children,
|
|
1868
|
-
redirectTo = "/login",
|
|
1869
|
-
tokenKey = "access_token",
|
|
1870
|
-
refreshToken
|
|
1871
|
-
}) => {
|
|
1872
|
-
const manager = (0, import_react_token_manager2.useTokenManager)();
|
|
1873
|
-
const router = (0, import_navigation.useRouter)();
|
|
1874
|
-
const isAuthChecked = useAuthStore((state) => state.isAuthChecked);
|
|
1875
|
-
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
|
1876
|
-
const error = useAuthStore((state) => state.error);
|
|
1877
|
-
(0, import_react2.useEffect)(() => {
|
|
1878
|
-
const checkAuth = async () => {
|
|
1879
|
-
try {
|
|
1880
|
-
let token = manager.getSingleToken(tokenKey);
|
|
1881
|
-
if (!token || manager.isExpired(token)) {
|
|
1882
|
-
if (refreshToken) {
|
|
1883
|
-
const newToken = await refreshToken();
|
|
1884
|
-
if (newToken) {
|
|
1885
|
-
manager.setTokens({ [tokenKey]: newToken });
|
|
1886
|
-
token = newToken;
|
|
1887
|
-
}
|
|
1888
|
-
}
|
|
1889
|
-
}
|
|
1890
|
-
const isValid = token && !manager.isExpired(token);
|
|
1891
|
-
useAuthStore.getState().setAuth(Boolean(isValid));
|
|
1892
|
-
if (!isValid) {
|
|
1893
|
-
router.replace(redirectTo);
|
|
1894
|
-
}
|
|
1895
|
-
} catch (err) {
|
|
1896
|
-
useAuthStore.getState().setError(err instanceof Error ? err : new Error(String(err)));
|
|
1897
|
-
useAuthStore.getState().setAuth(false);
|
|
1898
|
-
router.replace(redirectTo);
|
|
1899
|
-
} finally {
|
|
1900
|
-
useAuthStore.getState().setAuthChecked(true);
|
|
1901
|
-
}
|
|
1902
|
-
};
|
|
1903
|
-
checkAuth();
|
|
1904
|
-
}, [manager, router, redirectTo, tokenKey, refreshToken]);
|
|
1905
|
-
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Loading..." });
|
|
1906
|
-
if (error) return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
|
|
1907
|
-
"Error: ",
|
|
1908
|
-
error.message
|
|
1909
|
-
] });
|
|
1910
|
-
if (!isAuthenticated) return null;
|
|
1911
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
1912
|
-
};
|
|
1913
|
-
var AuthGuard_default = AuthGuard;
|
|
1914
|
-
|
|
1915
|
-
// src/RoleGuard.tsx
|
|
1916
|
-
var import_react3 = __toESM(require_react());
|
|
1917
|
-
var import_navigation2 = require("next/navigation");
|
|
1918
|
-
|
|
1919
|
-
// src/myAuth.ts
|
|
1920
|
-
var auth = createAppAuth();
|
|
1921
|
-
|
|
1922
|
-
// src/RoleGuard.tsx
|
|
1923
|
-
var import_jsx_runtime3 = __toESM(require_jsx_runtime());
|
|
1924
|
-
var { useAuth } = auth;
|
|
1925
|
-
var RoleGuard = ({
|
|
1926
|
-
children,
|
|
1927
|
-
allowedRoles,
|
|
1928
|
-
redirectTo = "/unauthorized"
|
|
1929
|
-
}) => {
|
|
1930
|
-
const { user, loading } = useAuth();
|
|
1931
|
-
const router = (0, import_navigation2.useRouter)();
|
|
1932
|
-
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1933
|
-
(0, import_react3.useEffect)(() => {
|
|
1934
|
-
if (!user) return;
|
|
1935
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1936
|
-
if (!hasAccess) {
|
|
1937
|
-
router.replace(redirectTo);
|
|
1938
|
-
}
|
|
1939
|
-
setIsChecking(false);
|
|
1940
|
-
}, [user, allowedRoles, redirectTo, router]);
|
|
1941
|
-
if (loading || !user || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1942
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1943
|
-
};
|
|
1944
|
-
var RoleGuard_default = RoleGuard;
|
|
1945
|
-
|
|
1946
|
-
// src/index.ts
|
|
1947
|
-
function createAppAuth(storage = "cookie") {
|
|
1948
|
-
return createAuthContext({ storage });
|
|
1855
|
+
return { AuthProvider, useAuth };
|
|
1949
1856
|
}
|
|
1950
1857
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1951
1858
|
0 && (module.exports = {
|
|
1952
|
-
AuthGuard,
|
|
1953
|
-
RoleGuard,
|
|
1954
|
-
createAppAuth,
|
|
1955
1859
|
createAuthContext
|
|
1956
1860
|
});
|
|
1957
1861
|
/*! Bundled license information:
|
package/dist/index.mjs
CHANGED
|
@@ -1693,18 +1693,18 @@ var require_react_jsx_runtime_development = __commonJS({
|
|
|
1693
1693
|
function isValidElement(object) {
|
|
1694
1694
|
return "object" === typeof object && null !== object && object.$$typeof === REACT_ELEMENT_TYPE;
|
|
1695
1695
|
}
|
|
1696
|
-
var
|
|
1696
|
+
var React2 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React2.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
|
|
1697
1697
|
return null;
|
|
1698
1698
|
};
|
|
1699
|
-
|
|
1699
|
+
React2 = {
|
|
1700
1700
|
react_stack_bottom_frame: function(callStackForError) {
|
|
1701
1701
|
return callStackForError();
|
|
1702
1702
|
}
|
|
1703
1703
|
};
|
|
1704
1704
|
var specialPropKeyWarningShown;
|
|
1705
1705
|
var didWarnAboutElementRef = {};
|
|
1706
|
-
var unknownOwnerDebugStack =
|
|
1707
|
-
|
|
1706
|
+
var unknownOwnerDebugStack = React2.react_stack_bottom_frame.bind(
|
|
1707
|
+
React2,
|
|
1708
1708
|
UnknownOwner
|
|
1709
1709
|
)();
|
|
1710
1710
|
var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
|
|
@@ -1834,107 +1834,14 @@ function createAuthContext(options) {
|
|
|
1834
1834
|
}
|
|
1835
1835
|
);
|
|
1836
1836
|
};
|
|
1837
|
-
const
|
|
1837
|
+
const useAuth = () => {
|
|
1838
1838
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
1839
1839
|
if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
|
|
1840
1840
|
return ctx;
|
|
1841
1841
|
};
|
|
1842
|
-
return { AuthProvider, useAuth
|
|
1843
|
-
}
|
|
1844
|
-
|
|
1845
|
-
// src/AuthGuard.tsx
|
|
1846
|
-
var import_react2 = __toESM(require_react());
|
|
1847
|
-
import { useRouter } from "next/navigation";
|
|
1848
|
-
import { useTokenManager as useTokenManager2 } from "react-token-manager";
|
|
1849
|
-
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1850
|
-
var AuthGuard = ({
|
|
1851
|
-
children,
|
|
1852
|
-
redirectTo = "/login",
|
|
1853
|
-
tokenKey = "access_token",
|
|
1854
|
-
refreshToken
|
|
1855
|
-
}) => {
|
|
1856
|
-
const manager = useTokenManager2();
|
|
1857
|
-
const router = useRouter();
|
|
1858
|
-
const isAuthChecked = useAuthStore((state) => state.isAuthChecked);
|
|
1859
|
-
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
|
|
1860
|
-
const error = useAuthStore((state) => state.error);
|
|
1861
|
-
(0, import_react2.useEffect)(() => {
|
|
1862
|
-
const checkAuth = async () => {
|
|
1863
|
-
try {
|
|
1864
|
-
let token = manager.getSingleToken(tokenKey);
|
|
1865
|
-
if (!token || manager.isExpired(token)) {
|
|
1866
|
-
if (refreshToken) {
|
|
1867
|
-
const newToken = await refreshToken();
|
|
1868
|
-
if (newToken) {
|
|
1869
|
-
manager.setTokens({ [tokenKey]: newToken });
|
|
1870
|
-
token = newToken;
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
}
|
|
1874
|
-
const isValid = token && !manager.isExpired(token);
|
|
1875
|
-
useAuthStore.getState().setAuth(Boolean(isValid));
|
|
1876
|
-
if (!isValid) {
|
|
1877
|
-
router.replace(redirectTo);
|
|
1878
|
-
}
|
|
1879
|
-
} catch (err) {
|
|
1880
|
-
useAuthStore.getState().setError(err instanceof Error ? err : new Error(String(err)));
|
|
1881
|
-
useAuthStore.getState().setAuth(false);
|
|
1882
|
-
router.replace(redirectTo);
|
|
1883
|
-
} finally {
|
|
1884
|
-
useAuthStore.getState().setAuthChecked(true);
|
|
1885
|
-
}
|
|
1886
|
-
};
|
|
1887
|
-
checkAuth();
|
|
1888
|
-
}, [manager, router, redirectTo, tokenKey, refreshToken]);
|
|
1889
|
-
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Loading..." });
|
|
1890
|
-
if (error) return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
|
|
1891
|
-
"Error: ",
|
|
1892
|
-
error.message
|
|
1893
|
-
] });
|
|
1894
|
-
if (!isAuthenticated) return null;
|
|
1895
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
1896
|
-
};
|
|
1897
|
-
var AuthGuard_default = AuthGuard;
|
|
1898
|
-
|
|
1899
|
-
// src/RoleGuard.tsx
|
|
1900
|
-
var import_react3 = __toESM(require_react());
|
|
1901
|
-
import { useRouter as useRouter2 } from "next/navigation";
|
|
1902
|
-
|
|
1903
|
-
// src/myAuth.ts
|
|
1904
|
-
var auth = createAppAuth();
|
|
1905
|
-
|
|
1906
|
-
// src/RoleGuard.tsx
|
|
1907
|
-
var import_jsx_runtime3 = __toESM(require_jsx_runtime());
|
|
1908
|
-
var { useAuth } = auth;
|
|
1909
|
-
var RoleGuard = ({
|
|
1910
|
-
children,
|
|
1911
|
-
allowedRoles,
|
|
1912
|
-
redirectTo = "/unauthorized"
|
|
1913
|
-
}) => {
|
|
1914
|
-
const { user, loading } = useAuth();
|
|
1915
|
-
const router = useRouter2();
|
|
1916
|
-
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1917
|
-
(0, import_react3.useEffect)(() => {
|
|
1918
|
-
if (!user) return;
|
|
1919
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1920
|
-
if (!hasAccess) {
|
|
1921
|
-
router.replace(redirectTo);
|
|
1922
|
-
}
|
|
1923
|
-
setIsChecking(false);
|
|
1924
|
-
}, [user, allowedRoles, redirectTo, router]);
|
|
1925
|
-
if (loading || !user || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1926
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1927
|
-
};
|
|
1928
|
-
var RoleGuard_default = RoleGuard;
|
|
1929
|
-
|
|
1930
|
-
// src/index.ts
|
|
1931
|
-
function createAppAuth(storage = "cookie") {
|
|
1932
|
-
return createAuthContext({ storage });
|
|
1842
|
+
return { AuthProvider, useAuth };
|
|
1933
1843
|
}
|
|
1934
1844
|
export {
|
|
1935
|
-
AuthGuard_default as AuthGuard,
|
|
1936
|
-
RoleGuard_default as RoleGuard,
|
|
1937
|
-
createAppAuth,
|
|
1938
1845
|
createAuthContext
|
|
1939
1846
|
};
|
|
1940
1847
|
/*! Bundled license information:
|
package/package.json
CHANGED
package/src/AuthGuard.tsx
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
'use client'
|
|
1
|
+
// 'use client'
|
|
2
2
|
|
|
3
|
-
import React, { useEffect, useState } from 'react'
|
|
4
|
-
import { useRouter } from 'next/navigation'
|
|
5
|
-
import { useTokenManager } from 'react-token-manager'
|
|
6
|
-
import { useAuthStore } from '../store/useGuardStore'
|
|
3
|
+
// import React, { useEffect, useState } from 'react'
|
|
4
|
+
// import { useRouter } from 'next/navigation'
|
|
5
|
+
// import { useTokenManager } from 'react-token-manager'
|
|
6
|
+
// import { useAuthStore } from '../store/useGuardStore'
|
|
7
7
|
|
|
8
|
-
type AuthGuardProps = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
8
|
+
// type AuthGuardProps = {
|
|
9
|
+
// children: React.ReactNode
|
|
10
|
+
// redirectTo?: string
|
|
11
|
+
// tokenKey?: string
|
|
12
|
+
// refreshToken?: () => Promise<string | null>
|
|
13
|
+
// }
|
|
14
14
|
|
|
15
|
-
const AuthGuard = ({
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}: AuthGuardProps) => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
// const AuthGuard = ({
|
|
16
|
+
// children,
|
|
17
|
+
// redirectTo = '/login',
|
|
18
|
+
// tokenKey = 'access_token',
|
|
19
|
+
// refreshToken,
|
|
20
|
+
// }: AuthGuardProps) => {
|
|
21
|
+
// const manager = useTokenManager()
|
|
22
|
+
// const router = useRouter()
|
|
23
|
+
// const isAuthChecked = useAuthStore((state) => state.isAuthChecked)
|
|
24
|
+
// const isAuthenticated = useAuthStore((state) => state.isAuthenticated)
|
|
25
|
+
// const error = useAuthStore((state) => state.error)
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
// useEffect(() => {
|
|
28
|
+
// const checkAuth = async () => {
|
|
29
|
+
// try {
|
|
30
|
+
// let token = manager.getSingleToken(tokenKey)
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
// if (!token || manager.isExpired(token)) {
|
|
33
|
+
// if (refreshToken) {
|
|
34
|
+
// const newToken = await refreshToken()
|
|
35
|
+
// if (newToken) {
|
|
36
|
+
// manager.setTokens({ [tokenKey]: newToken })
|
|
37
|
+
// token = newToken
|
|
38
|
+
// }
|
|
39
|
+
// }
|
|
40
|
+
// }
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
// const isValid = token && !manager.isExpired(token)
|
|
43
|
+
// useAuthStore.getState().setAuth(Boolean(isValid))
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
45
|
+
// if (!isValid) {
|
|
46
|
+
// router.replace(redirectTo)
|
|
47
|
+
// }
|
|
48
|
+
// } catch (err: any) {
|
|
49
|
+
// useAuthStore.getState().setError(err instanceof Error ? err : new Error(String(err)))
|
|
50
|
+
// useAuthStore.getState().setAuth(false)
|
|
51
|
+
// router.replace(redirectTo)
|
|
52
|
+
// } finally {
|
|
53
|
+
// useAuthStore.getState().setAuthChecked(true)
|
|
54
|
+
// }
|
|
55
|
+
// }
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
// checkAuth()
|
|
58
|
+
// }, [manager, router, redirectTo, tokenKey, refreshToken])
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
// if (!isAuthChecked) return <div>Loading...</div>
|
|
61
|
+
// if (error) return <div>Error: {error.message}</div>
|
|
62
|
+
// if (!isAuthenticated) return null
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
}
|
|
64
|
+
// return <>{children}</>
|
|
65
|
+
// }
|
|
66
66
|
|
|
67
|
-
export default AuthGuard
|
|
67
|
+
// export default AuthGuard
|
package/src/RoleGuard.tsx
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
|
-
'use client'
|
|
2
|
-
|
|
3
|
-
import React, { useEffect, useState } from 'react'
|
|
4
|
-
import { useRouter } from 'next/navigation'
|
|
5
|
-
import { auth } from './myAuth'
|
|
6
|
-
|
|
7
|
-
type RoleGuardProps = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
1
|
+
// 'use client'
|
|
2
|
+
|
|
3
|
+
// import React, { useEffect, useState } from 'react'
|
|
4
|
+
// import { useRouter } from 'next/navigation'
|
|
5
|
+
// import { auth } from './myAuth'
|
|
6
|
+
|
|
7
|
+
// type RoleGuardProps = {
|
|
8
|
+
// children: React.ReactNode
|
|
9
|
+
// allowedRoles: string[]
|
|
10
|
+
// redirectTo?: string
|
|
11
|
+
// }
|
|
12
|
+
|
|
13
|
+
// const RoleGuard: React.FC<RoleGuardProps> = ({
|
|
14
|
+
// children,
|
|
15
|
+
// allowedRoles,
|
|
16
|
+
// redirectTo = '/unauthorized',
|
|
17
|
+
// }) => {
|
|
18
|
+
// // ✅ Destructure useAuth INSIDE the component
|
|
19
|
+
// const { useAuth } = auth
|
|
20
|
+
// const { user, loading } = useAuth()
|
|
21
|
+
|
|
22
|
+
// const router = useRouter()
|
|
23
|
+
// const [isChecking, setIsChecking] = useState(true)
|
|
24
|
+
|
|
25
|
+
// useEffect(() => {
|
|
26
|
+
// if (!user) return
|
|
27
|
+
|
|
28
|
+
// const hasAccess = allowedRoles.includes(user?.role)
|
|
29
|
+
// if (!hasAccess) {
|
|
30
|
+
// router.replace(redirectTo)
|
|
31
|
+
// }
|
|
32
|
+
// setIsChecking(false)
|
|
33
|
+
// }, [user, allowedRoles, redirectTo, router])
|
|
34
|
+
|
|
35
|
+
// if (loading || !user || isChecking) return <div>Loading...</div>
|
|
36
|
+
|
|
37
|
+
// return <>{children}</>
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
// export default RoleGuard
|
package/src/index.ts
CHANGED
|
@@ -5,12 +5,12 @@ export type User = {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
// Export factory instead of fixed instance
|
|
8
|
-
export function createAppAuth(
|
|
9
|
-
|
|
10
|
-
) {
|
|
11
|
-
|
|
12
|
-
}
|
|
8
|
+
// export function createAppAuth(
|
|
9
|
+
// storage: 'localStorage' | 'sessionStorage' | 'cookie' = 'cookie'
|
|
10
|
+
// ) {
|
|
11
|
+
// return createAuthContext<User>({ storage })
|
|
12
|
+
// }
|
|
13
13
|
|
|
14
14
|
export { createAuthContext } from './AuthProvider'
|
|
15
|
-
export { default as AuthGuard } from './AuthGuard'
|
|
16
|
-
export { default as RoleGuard } from './RoleGuard'
|
|
15
|
+
// export { default as AuthGuard } from './AuthGuard'
|
|
16
|
+
// export { default as RoleGuard } from './RoleGuard'
|
package/src/myAuth.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
// myAuth.ts
|
|
2
|
-
'use client'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export type User = any
|
|
7
|
-
|
|
8
|
-
// Export the factory
|
|
9
|
-
export const auth = createAppAuth() // { AuthProvider, useAuth }
|
|
1
|
+
// // myAuth.ts
|
|
2
|
+
// 'use client'
|
|
3
|
+
// import { createAppAuth } from '.'
|
|
4
|
+
// export const auth = createAppAuth() // { AuthProvider, useAuth }
|