@authaz/next 0.1.0 → 1.0.2
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.js +15 -13
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
1
|
import { AuthazError, COOKIE_NAMES, createAuthazClient, fetchUserinfo, getSecureCookieOptions, isOk, mapUserinfoToUser, timingSafeCompare } from "@authaz/sdk";
|
|
3
2
|
import { NextResponse } from "next/server";
|
|
4
3
|
|
|
5
|
-
//#region rolldown:runtime
|
|
6
|
-
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
7
|
-
|
|
8
|
-
//#endregion
|
|
9
4
|
//#region src/index.tsx
|
|
10
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Lazy load next/headers at runtime to avoid MODULE_NOT_FOUND on Vercel/serverless
|
|
7
|
+
* when the package is resolved from a different node_modules context (e.g. pnpm).
|
|
8
|
+
*/
|
|
9
|
+
const getCookieStore = async () => {
|
|
10
|
+
const { cookies } = await import("next/headers");
|
|
11
|
+
return cookies();
|
|
12
|
+
};
|
|
11
13
|
const isRequestHttps = (request) => {
|
|
12
14
|
if (request.headers.get("x-forwarded-proto") === "https") return true;
|
|
13
15
|
return new URL(request.url).protocol === "https:";
|
|
@@ -163,7 +165,7 @@ const createAuthazHandler = (config) => {
|
|
|
163
165
|
logError("Missing authorization code");
|
|
164
166
|
return NextResponse.redirect(`${baseUrl}${afterLoginUrl}?error=missing_code`);
|
|
165
167
|
}
|
|
166
|
-
const cookieStore = await
|
|
168
|
+
const cookieStore = await getCookieStore();
|
|
167
169
|
const codeVerifier = cookieStore.get(COOKIE_NAMES.CODE_VERIFIER)?.value;
|
|
168
170
|
const storedState = cookieStore.get(COOKIE_NAMES.STATE)?.value;
|
|
169
171
|
log("All cookies", cookieStore.getAll().map((c) => c.name));
|
|
@@ -228,7 +230,7 @@ const createAuthazHandler = (config) => {
|
|
|
228
230
|
};
|
|
229
231
|
const handleMe = async () => {
|
|
230
232
|
log("Getting current user");
|
|
231
|
-
const accessToken = (await
|
|
233
|
+
const accessToken = (await getCookieStore()).get(COOKIE_NAMES.ACCESS_TOKEN)?.value;
|
|
232
234
|
if (!accessToken) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
233
235
|
const userinfo = await fetchUserinfo(authazDomain, accessToken, apiKey);
|
|
234
236
|
if (!userinfo) {
|
|
@@ -243,7 +245,7 @@ const createAuthazHandler = (config) => {
|
|
|
243
245
|
};
|
|
244
246
|
const handleRefresh = async (request) => {
|
|
245
247
|
log("Handling token refresh");
|
|
246
|
-
const refreshToken = (await
|
|
248
|
+
const refreshToken = (await getCookieStore()).get(COOKIE_NAMES.REFRESH_TOKEN)?.value;
|
|
247
249
|
if (!refreshToken) return NextResponse.json({ error: "No refresh token" }, { status: 401 });
|
|
248
250
|
const result = await getClient().auth.refreshTokens(refreshToken);
|
|
249
251
|
if (!isOk(result)) {
|
|
@@ -294,13 +296,13 @@ const createAuthazHandler = (config) => {
|
|
|
294
296
|
* Gets the current access token from cookies.
|
|
295
297
|
*/
|
|
296
298
|
const getAccessToken = async () => {
|
|
297
|
-
return (await
|
|
299
|
+
return (await getCookieStore()).get(COOKIE_NAMES.ACCESS_TOKEN)?.value || null;
|
|
298
300
|
};
|
|
299
301
|
/**
|
|
300
302
|
* Gets the current refresh token from cookies.
|
|
301
303
|
*/
|
|
302
304
|
const getRefreshToken = async () => {
|
|
303
|
-
return (await
|
|
305
|
+
return (await getCookieStore()).get(COOKIE_NAMES.REFRESH_TOKEN)?.value || null;
|
|
304
306
|
};
|
|
305
307
|
/**
|
|
306
308
|
* Checks if the user is authenticated (has an access token).
|
|
@@ -315,7 +317,7 @@ const createAuthazHelpers = (config) => {
|
|
|
315
317
|
const authazDomain = config.authazDomain || "https://authaz.com";
|
|
316
318
|
const apiKey = config.apiKey || config.clientSecret;
|
|
317
319
|
const getUser = async () => {
|
|
318
|
-
const accessToken = (await
|
|
320
|
+
const accessToken = (await getCookieStore()).get(COOKIE_NAMES.ACCESS_TOKEN)?.value;
|
|
319
321
|
if (!accessToken) return null;
|
|
320
322
|
const userinfo = await fetchUserinfo(authazDomain, accessToken, apiKey);
|
|
321
323
|
if (!userinfo) return null;
|
|
@@ -427,7 +429,7 @@ const requireUser = (config) => {
|
|
|
427
429
|
const loginPath = config.loginPath || "/api/auth/login";
|
|
428
430
|
const getOrRedirect = async () => {
|
|
429
431
|
const navigation = await import("next/navigation");
|
|
430
|
-
const accessToken = (await
|
|
432
|
+
const accessToken = (await getCookieStore()).get(COOKIE_NAMES.ACCESS_TOKEN)?.value;
|
|
431
433
|
if (!accessToken) navigation.redirect(loginPath);
|
|
432
434
|
const userinfo = await fetchUserinfo(authazDomain, accessToken, apiKey);
|
|
433
435
|
if (!userinfo) navigation.redirect(loginPath);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "@authaz",
|
|
3
3
|
"name": "@authaz/next",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "NextJS authaz SDK",
|
|
7
7
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"next": ">=15",
|
|
41
41
|
"react": ">=17",
|
|
42
|
-
"@authaz/sdk": "^1.2.
|
|
42
|
+
"@authaz/sdk": "^1.2.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@jest/globals": "30.2.0",
|