aaspai-authx 0.0.5 → 0.0.6
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.cjs +2 -217
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -112
- package/dist/index.d.ts +1 -112
- package/dist/index.js +1 -205
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -40,21 +40,15 @@ var src_exports = {};
|
|
|
40
40
|
__export(src_exports, {
|
|
41
41
|
AuthAdminService: () => AuthAdminService,
|
|
42
42
|
AuthXGuard: () => AuthXGuard,
|
|
43
|
-
AuthXProvider: () => AuthXProvider,
|
|
44
43
|
AuthXSessionDecorator: () => AuthXSessionDecorator,
|
|
45
44
|
AuthXStrategy: () => AuthXStrategy,
|
|
46
45
|
EmailService: () => EmailService,
|
|
47
|
-
HasPermission: () => HasPermission,
|
|
48
|
-
HasRole: () => HasRole,
|
|
49
46
|
PLATFORM_ROLES: () => PLATFORM_ROLES,
|
|
50
47
|
Permissions: () => Permissions,
|
|
51
48
|
ProjectsService: () => ProjectsService,
|
|
52
49
|
Roles: () => Roles,
|
|
53
|
-
SignedIn: () => SignedIn,
|
|
54
|
-
SignedOut: () => SignedOut,
|
|
55
50
|
UploadsService: () => UploadsService,
|
|
56
51
|
authorize: () => authorize,
|
|
57
|
-
authx: () => authx,
|
|
58
52
|
buildSession: () => buildSession,
|
|
59
53
|
createAuthXStrategy: () => createAuthXStrategy,
|
|
60
54
|
express: () => express_exports,
|
|
@@ -69,12 +63,7 @@ __export(src_exports, {
|
|
|
69
63
|
requireAuth: () => requireAuth,
|
|
70
64
|
requirePermission: () => requirePermission2,
|
|
71
65
|
requirePermissionLegacy: () => requirePermission,
|
|
72
|
-
requireRole: () => requireRole
|
|
73
|
-
useAuthX: () => useAuthX,
|
|
74
|
-
useAuthXContext: () => useAuthXContext,
|
|
75
|
-
useHasPermission: () => useHasPermission,
|
|
76
|
-
useHasRole: () => useHasRole,
|
|
77
|
-
withAuthRoute: () => withAuthRoute
|
|
66
|
+
requireRole: () => requireRole
|
|
78
67
|
});
|
|
79
68
|
module.exports = __toCommonJS(src_exports);
|
|
80
69
|
|
|
@@ -2064,218 +2053,19 @@ var AuthXStrategy = class extends import_passport2.Strategy {
|
|
|
2064
2053
|
function createAuthXStrategy() {
|
|
2065
2054
|
return new AuthXStrategy();
|
|
2066
2055
|
}
|
|
2067
|
-
|
|
2068
|
-
// src/next/server/authx.ts
|
|
2069
|
-
var import_headers = require("next/headers");
|
|
2070
|
-
async function authx() {
|
|
2071
|
-
try {
|
|
2072
|
-
const cookieStore = await (0, import_headers.cookies)();
|
|
2073
|
-
const token = cookieStore.get("access_token")?.value || cookieStore.get("authorization")?.value || cookieStore.get("auth_token")?.value || null;
|
|
2074
|
-
if (!token) {
|
|
2075
|
-
return { session: null };
|
|
2076
|
-
}
|
|
2077
|
-
const claims = await verifyJwt(token);
|
|
2078
|
-
const session = buildSession(claims);
|
|
2079
|
-
return { session };
|
|
2080
|
-
} catch (error) {
|
|
2081
|
-
return { session: null };
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
|
|
2085
|
-
// src/next/server/withAuthRoute.ts
|
|
2086
|
-
var import_server = require("next/server");
|
|
2087
|
-
function withAuthRoute(permissionOrHandler, handler) {
|
|
2088
|
-
let permission;
|
|
2089
|
-
let routeHandler;
|
|
2090
|
-
if (typeof permissionOrHandler === "string") {
|
|
2091
|
-
permission = permissionOrHandler;
|
|
2092
|
-
routeHandler = handler;
|
|
2093
|
-
} else {
|
|
2094
|
-
permission = void 0;
|
|
2095
|
-
routeHandler = permissionOrHandler;
|
|
2096
|
-
}
|
|
2097
|
-
return async (req, context) => {
|
|
2098
|
-
const { session } = await authx();
|
|
2099
|
-
if (!session) {
|
|
2100
|
-
return new import_server.NextResponse(
|
|
2101
|
-
JSON.stringify({ error: "Unauthorized" }),
|
|
2102
|
-
{ status: 401, headers: { "Content-Type": "application/json" } }
|
|
2103
|
-
);
|
|
2104
|
-
}
|
|
2105
|
-
if (permission && !hasPermission(session, permission)) {
|
|
2106
|
-
return new import_server.NextResponse(
|
|
2107
|
-
JSON.stringify({
|
|
2108
|
-
error: "Forbidden",
|
|
2109
|
-
reason: "MISSING_PERMISSION",
|
|
2110
|
-
permission
|
|
2111
|
-
}),
|
|
2112
|
-
{ status: 403, headers: { "Content-Type": "application/json" } }
|
|
2113
|
-
);
|
|
2114
|
-
}
|
|
2115
|
-
return routeHandler(req, session, context);
|
|
2116
|
-
};
|
|
2117
|
-
}
|
|
2118
|
-
|
|
2119
|
-
// src/next/client/AuthXProvider.tsx
|
|
2120
|
-
var import_react = require("react");
|
|
2121
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
2122
|
-
var AuthXContext = (0, import_react.createContext)(void 0);
|
|
2123
|
-
function AuthXProvider({ children, apiUrl = "/auth/me" }) {
|
|
2124
|
-
const [session, setSession] = (0, import_react.useState)(null);
|
|
2125
|
-
const [isLoading, setIsLoading] = (0, import_react.useState)(true);
|
|
2126
|
-
const [error, setError] = (0, import_react.useState)(null);
|
|
2127
|
-
const fetchSession = async () => {
|
|
2128
|
-
try {
|
|
2129
|
-
setIsLoading(true);
|
|
2130
|
-
setError(null);
|
|
2131
|
-
const response = await fetch(apiUrl, {
|
|
2132
|
-
credentials: "include"
|
|
2133
|
-
// Include cookies
|
|
2134
|
-
});
|
|
2135
|
-
if (!response.ok) {
|
|
2136
|
-
if (response.status === 401) {
|
|
2137
|
-
setSession(null);
|
|
2138
|
-
return;
|
|
2139
|
-
}
|
|
2140
|
-
throw new Error(`Failed to fetch session: ${response.statusText}`);
|
|
2141
|
-
}
|
|
2142
|
-
const data = await response.json();
|
|
2143
|
-
setSession(data.user || data.session || data);
|
|
2144
|
-
} catch (err) {
|
|
2145
|
-
setError(err instanceof Error ? err : new Error("Unknown error"));
|
|
2146
|
-
setSession(null);
|
|
2147
|
-
} finally {
|
|
2148
|
-
setIsLoading(false);
|
|
2149
|
-
}
|
|
2150
|
-
};
|
|
2151
|
-
(0, import_react.useEffect)(() => {
|
|
2152
|
-
fetchSession();
|
|
2153
|
-
}, [apiUrl]);
|
|
2154
|
-
const value = {
|
|
2155
|
-
session,
|
|
2156
|
-
isLoading,
|
|
2157
|
-
error,
|
|
2158
|
-
refetch: fetchSession
|
|
2159
|
-
};
|
|
2160
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthXContext.Provider, { value, children });
|
|
2161
|
-
}
|
|
2162
|
-
function useAuthXContext() {
|
|
2163
|
-
const context = (0, import_react.useContext)(AuthXContext);
|
|
2164
|
-
if (context === void 0) {
|
|
2165
|
-
throw new Error("useAuthX must be used within an AuthXProvider");
|
|
2166
|
-
}
|
|
2167
|
-
return context;
|
|
2168
|
-
}
|
|
2169
|
-
|
|
2170
|
-
// src/next/client/HasPermission.tsx
|
|
2171
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
2172
|
-
function HasPermission({
|
|
2173
|
-
permission,
|
|
2174
|
-
children,
|
|
2175
|
-
fallback = null
|
|
2176
|
-
}) {
|
|
2177
|
-
const { session, isLoading } = useAuthXContext();
|
|
2178
|
-
if (isLoading) {
|
|
2179
|
-
return null;
|
|
2180
|
-
}
|
|
2181
|
-
if (!session) {
|
|
2182
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback });
|
|
2183
|
-
}
|
|
2184
|
-
const hasPerm = hasPermission(session, permission);
|
|
2185
|
-
if (!hasPerm) {
|
|
2186
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback });
|
|
2187
|
-
}
|
|
2188
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
2189
|
-
}
|
|
2190
|
-
|
|
2191
|
-
// src/next/client/HasRole.tsx
|
|
2192
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
2193
|
-
function HasRole({ role, children, fallback = null }) {
|
|
2194
|
-
const { session, isLoading } = useAuthXContext();
|
|
2195
|
-
if (isLoading) {
|
|
2196
|
-
return null;
|
|
2197
|
-
}
|
|
2198
|
-
if (!session) {
|
|
2199
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
2200
|
-
}
|
|
2201
|
-
const roles = Array.isArray(role) ? role : [role];
|
|
2202
|
-
const hasRole2 = hasAnyRole(session, roles);
|
|
2203
|
-
if (!hasRole2) {
|
|
2204
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
2205
|
-
}
|
|
2206
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
2207
|
-
}
|
|
2208
|
-
|
|
2209
|
-
// src/next/client/SignedIn.tsx
|
|
2210
|
-
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
2211
|
-
function SignedIn({ children, fallback = null }) {
|
|
2212
|
-
const { session, isLoading } = useAuthXContext();
|
|
2213
|
-
if (isLoading) {
|
|
2214
|
-
return null;
|
|
2215
|
-
}
|
|
2216
|
-
if (!session) {
|
|
2217
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: fallback });
|
|
2218
|
-
}
|
|
2219
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children });
|
|
2220
|
-
}
|
|
2221
|
-
|
|
2222
|
-
// src/next/client/SignedOut.tsx
|
|
2223
|
-
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
2224
|
-
function SignedOut({ children, fallback = null }) {
|
|
2225
|
-
const { session, isLoading } = useAuthXContext();
|
|
2226
|
-
if (isLoading) {
|
|
2227
|
-
return null;
|
|
2228
|
-
}
|
|
2229
|
-
if (session) {
|
|
2230
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children: fallback });
|
|
2231
|
-
}
|
|
2232
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_jsx_runtime5.Fragment, { children });
|
|
2233
|
-
}
|
|
2234
|
-
|
|
2235
|
-
// src/next/client/useAuthX.ts
|
|
2236
|
-
function useAuthX() {
|
|
2237
|
-
const { session, isLoading, error, refetch } = useAuthXContext();
|
|
2238
|
-
return {
|
|
2239
|
-
session,
|
|
2240
|
-
isLoading,
|
|
2241
|
-
error,
|
|
2242
|
-
refetch,
|
|
2243
|
-
isSignedIn: !!session,
|
|
2244
|
-
userId: session?.userId || null,
|
|
2245
|
-
email: session?.email || null,
|
|
2246
|
-
roles: session?.roles || [],
|
|
2247
|
-
permissions: session?.permissions || []
|
|
2248
|
-
};
|
|
2249
|
-
}
|
|
2250
|
-
function useHasRole(role) {
|
|
2251
|
-
const { session } = useAuthXContext();
|
|
2252
|
-
if (!session || !session.roles) return false;
|
|
2253
|
-
return session.roles.includes(role);
|
|
2254
|
-
}
|
|
2255
|
-
function useHasPermission(permission) {
|
|
2256
|
-
const { session } = useAuthXContext();
|
|
2257
|
-
if (!session || !session.permissions) return false;
|
|
2258
|
-
return session.permissions.includes(permission);
|
|
2259
|
-
}
|
|
2260
2056
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2261
2057
|
0 && (module.exports = {
|
|
2262
2058
|
AuthAdminService,
|
|
2263
2059
|
AuthXGuard,
|
|
2264
|
-
AuthXProvider,
|
|
2265
2060
|
AuthXSessionDecorator,
|
|
2266
2061
|
AuthXStrategy,
|
|
2267
2062
|
EmailService,
|
|
2268
|
-
HasPermission,
|
|
2269
|
-
HasRole,
|
|
2270
2063
|
PLATFORM_ROLES,
|
|
2271
2064
|
Permissions,
|
|
2272
2065
|
ProjectsService,
|
|
2273
2066
|
Roles,
|
|
2274
|
-
SignedIn,
|
|
2275
|
-
SignedOut,
|
|
2276
2067
|
UploadsService,
|
|
2277
2068
|
authorize,
|
|
2278
|
-
authx,
|
|
2279
2069
|
buildSession,
|
|
2280
2070
|
createAuthXStrategy,
|
|
2281
2071
|
express,
|
|
@@ -2290,11 +2080,6 @@ function useHasPermission(permission) {
|
|
|
2290
2080
|
requireAuth,
|
|
2291
2081
|
requirePermission,
|
|
2292
2082
|
requirePermissionLegacy,
|
|
2293
|
-
requireRole
|
|
2294
|
-
useAuthX,
|
|
2295
|
-
useAuthXContext,
|
|
2296
|
-
useHasPermission,
|
|
2297
|
-
useHasRole,
|
|
2298
|
-
withAuthRoute
|
|
2083
|
+
requireRole
|
|
2299
2084
|
});
|
|
2300
2085
|
//# sourceMappingURL=index.cjs.map
|