@clerk/nextjs
Version:
Clerk SDK for NextJS
65 lines • 2.78 kB
JavaScript
import "../../chunk-BUSYA2B4.js";
import { constants, createClerkRequest, createRedirect } from "@clerk/backend/internal";
import { notFound, redirect } from "next/navigation";
import { PUBLISHABLE_KEY, SIGN_IN_URL, SIGN_UP_URL } from "../../server/constants";
import { createAsyncGetAuth } from "../../server/createGetAuth";
import { authAuthHeaderMissing } from "../../server/errors";
import { getAuthKeyFromRequest, getHeader } from "../../server/headers-utils";
import { createProtect } from "../../server/protect";
import { decryptClerkRequestData } from "../../server/utils";
import { isNextWithUnstableServerActions } from "../../utils/sdk-versions";
import { buildRequestLike } from "./utils";
const auth = async () => {
require("server-only");
const request = await buildRequestLike();
const stepsBasedOnSrcDirectory = async () => {
if (isNextWithUnstableServerActions) {
return [];
}
try {
const isSrcAppDir = await import("../../server/fs/middleware-location.js").then((m) => m.hasSrcAppDir());
return [`Your Middleware exists at ./${isSrcAppDir ? "src/" : ""}middleware.(ts|js)`];
} catch {
return [];
}
};
const authObject = await createAsyncGetAuth({
debugLoggerName: "auth()",
noAuthStatusMessage: authAuthHeaderMissing("auth", await stepsBasedOnSrcDirectory())
})(request);
const clerkUrl = getAuthKeyFromRequest(request, "ClerkUrl");
const redirectToSignIn = (opts = {}) => {
const clerkRequest = createClerkRequest(request);
const devBrowserToken = clerkRequest.clerkUrl.searchParams.get(constants.QueryParameters.DevBrowser) || clerkRequest.cookies.get(constants.Cookies.DevBrowser);
const encryptedRequestData = getHeader(request, constants.Headers.ClerkRequestData);
const decryptedRequestData = decryptClerkRequestData(encryptedRequestData);
return createRedirect({
redirectAdapter: redirect,
devBrowserToken,
baseUrl: clerkRequest.clerkUrl.toString(),
publishableKey: decryptedRequestData.publishableKey || PUBLISHABLE_KEY,
signInUrl: decryptedRequestData.signInUrl || SIGN_IN_URL,
signUpUrl: decryptedRequestData.signUpUrl || SIGN_UP_URL
}).redirectToSignIn({
returnBackUrl: opts.returnBackUrl === null ? "" : opts.returnBackUrl || (clerkUrl == null ? void 0 : clerkUrl.toString())
});
};
return Object.assign(authObject, { redirectToSignIn });
};
auth.protect = async (...args) => {
require("server-only");
const request = await buildRequestLike();
const authObject = await auth();
const protect = createProtect({
request,
authObject,
redirectToSignIn: authObject.redirectToSignIn,
notFound,
redirect
});
return protect(...args);
};
export {
auth
};
//# sourceMappingURL=auth.js.map