UNPKG

clerk-solidjs

Version:
232 lines (225 loc) 9.81 kB
'use strict'; var web = require('solid-js/web'); var internal = require('@clerk/backend/internal'); var backend = require('@clerk/backend'); var error = require('@clerk/shared/error'); var shared = require('@clerk/shared'); var underscore = require('@clerk/shared/underscore'); var apiUrlFromPublishableKey = require('@clerk/shared/apiUrlFromPublishableKey'); var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null; // src/start/server/auth.ts exports.auth = () => { const event = web.getRequestEvent(); if (!event) { throw new Error("auth() must be called from within a server function"); } if (!event.locals.auth) { throw new Error("auth() returned null. Did you implement clerkMiddleware?"); } return event.locals.auth; }; var errorThrower = error.buildErrorThrower({ packageName: "clerk-solidjs" }); var getEnvVariable = (name, defaultVaue = "") => { if (typeof process !== "undefined" && process.env && typeof process.env[name] === "string") { return process.env[name] || defaultVaue; } if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)) }) !== "undefined" && // @ts-expect-error - Vite specific undefined && // @ts-expect-error - Vite specific typeof undefined[name] === "string") { return undefined[name]; } return defaultVaue; }; var getPublicEnvVariables = () => { return { publishableKey: getEnvVariable("VITE_CLERK_PUBLISHABLE_KEY") || getEnvVariable("CLERK_PUBLISHABLE_KEY"), domain: getEnvVariable("VITE_CLERK_DOMAIN") || getEnvVariable("CLERK_DOMAIN"), isSatellite: underscore.isTruthy(getEnvVariable("VITE_CLERK_IS_SATELLITE")) || underscore.isTruthy(getEnvVariable("CLERK_IS_SATELLITE")), proxyUrl: getEnvVariable("VITE_CLERK_PROXY_URL") || getEnvVariable("CLERK_PROXY_URL"), pk: getEnvVariable("VITE_CLERK_PUBLISHABLE_KEY") || getEnvVariable("CLERK_PUBLISHABLE_KEY"), signInUrl: getEnvVariable("VITE_CLERK_SIGN_IN_URL") || getEnvVariable("CLERK_SIGN_IN_URL"), signUpUrl: getEnvVariable("VITE_CLERK_SIGN_UP_URL") || getEnvVariable("CLERK_SIGN_UP_URL"), clerkJsUrl: getEnvVariable("VITE_CLERK_JS_URL") || getEnvVariable("CLERK_JS"), clerkJsVariant: getEnvVariable("VITE_CLERK_JS_VARIANT") || getEnvVariable("CLERK_JS_VARIANT"), clerkJsVersion: getEnvVariable("VITE_CLERK_JS_VERSION") || getEnvVariable("CLERK_JS_VERSION"), telemetryDisabled: underscore.isTruthy(getEnvVariable("VITE_CLERK_TELEMETRY_DISABLED")) || underscore.isTruthy(getEnvVariable("CLERK_TELEMETRY_DISABLED")), telemetryDebug: underscore.isTruthy(getEnvVariable("VITE_CLERK_TELEMETRY_DEBUG")) || underscore.isTruthy(getEnvVariable("CLERK_TELEMETRY_DEBUG")), afterSignInUrl: getEnvVariable("VITE_CLERK_AFTER_SIGN_IN_URL") || getEnvVariable("CLERK_AFTER_SIGN_IN_URL"), afterSignUpUrl: getEnvVariable("VITE_CLERK_AFTER_SIGN_UP_URL") || getEnvVariable("CLERK_AFTER_SIGN_UP_URL") }; }; // src/start/server/utils/index.ts var wrapWithClerkState = (data) => { return { __internal_clerk_state: { ...data } }; }; function getResponseClerkState(requestState, additionalStateOptions = {}) { const clerkInitialState = wrapWithClerkState({ __clerk_ssr_state: requestState.toAuth(), __publishableKey: requestState.publishableKey, __proxyUrl: requestState.proxyUrl, __domain: requestState.domain, __isSatellite: requestState.isSatellite, __signInUrl: requestState.signInUrl, __signUpUrl: requestState.signUpUrl, __afterSignInUrl: requestState.afterSignInUrl, __afterSignUpUrl: requestState.afterSignUpUrl, __clerk_debug: internal.debugRequestState(requestState), __clerkJSUrl: getEnvVariable("CLERK_JS"), __clerkJSVersion: getEnvVariable("CLERK_JS_VERSION"), __telemetryDisabled: shared.isTruthy(getEnvVariable("CLERK_TELEMETRY_DISABLED")), __telemetryDebug: shared.isTruthy(getEnvVariable("CLERK_TELEMETRY_DEBUG")), __signInForceRedirectUrl: additionalStateOptions.signInForceRedirectUrl || getEnvVariable("CLERK_SIGN_IN_FORCE_REDIRECT_URL") || "", __signUpForceRedirectUrl: additionalStateOptions.signUpForceRedirectUrl || getEnvVariable("CLERK_SIGN_UP_FORCE_REDIRECT_URL") || "", __signInFallbackRedirectUrl: additionalStateOptions.signInFallbackRedirectUrl || getEnvVariable("CLERK_SIGN_IN_FALLBACK_REDIRECT_URL") || "", __signUpFallbackRedirectUrl: additionalStateOptions.signUpFallbackRedirectUrl || getEnvVariable("CLERK_SIGN_UP_FALLBACK_REDIRECT_URL") || "" }); return { clerkInitialState, headers: requestState.headers }; } var patchRequest = (request) => { const clonedRequest = new Request(request.url, { headers: request.headers, method: request.method, redirect: request.redirect, cache: request.cache, signal: request.signal }); if (clonedRequest.method !== "GET" && clonedRequest.body !== null && !("duplex" in clonedRequest)) { clonedRequest.duplex = "half"; } return clonedRequest; }; // src/start/server/authenticate-request.ts async function authenticateRequest(request, opts) { const { audience, authorizedParties } = opts; const { apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey } = opts; const { signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } = opts; const requestState = await backend.createClerkClient({ apiUrl, secretKey, jwtKey, proxyUrl, isSatellite, domain, publishableKey, userAgent: `${"clerk-solidjs"}@${"2.0.10"}` }).authenticateRequest(patchRequest(request), { audience, authorizedParties, signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl }); const hasLocationHeader = requestState.headers.get("location"); if (hasLocationHeader) { throw new Response(null, { status: 307, headers: requestState.headers }); } if (requestState.status === internal.AuthStatus.Handshake) { throw errorThrower.throw("Clerk: unexpected handshake without redirect"); } return requestState; } getPublicEnvVariables().clerkJsVersion || ""; getPublicEnvVariables().clerkJsUrl || ""; getEnvVariable("CLERK_API_VERSION") || "v1"; var SECRET_KEY = getEnvVariable("CLERK_SECRET_KEY") || ""; var PUBLISHABLE_KEY = getPublicEnvVariables().publishableKey || ""; getEnvVariable("CLERK_ENCRYPTION_KEY") || ""; getEnvVariable("CLERK_API_URL") || apiUrlFromPublishableKey.apiUrlFromPublishableKey(PUBLISHABLE_KEY); var DOMAIN = getPublicEnvVariables().domain || ""; var PROXY_URL = getPublicEnvVariables().proxyUrl || ""; var CLERK_JWT_KEY = getEnvVariable("CLERK_JWT_KEY") || ""; var IS_SATELLITE = getPublicEnvVariables().isSatellite || false; var SIGN_IN_URL = getPublicEnvVariables().signInUrl || ""; var SIGN_UP_URL = getPublicEnvVariables().signUpUrl || ""; ({ name: "clerk-solidjs", version: "2.0.10", environment: getEnvVariable("NODE_ENV") }); getPublicEnvVariables().telemetryDisabled; getPublicEnvVariables().telemetryDebug; // src/start/server/load-options.ts var loadOptions = (request, overrides = {}) => { const clerkRequest = internal.createClerkRequest(patchRequest(request)); const secretKey = overrides.secretKey || SECRET_KEY; const publishableKey = overrides.publishableKey || PUBLISHABLE_KEY; const jwtKey = overrides.jwtKey || CLERK_JWT_KEY; const apiUrl = getEnvVariable("CLERK_API_URL") || shared.apiUrlFromPublishableKey(publishableKey); const domain = shared.handleValueOrFn(overrides.domain, new URL(request.url)) || DOMAIN; const isSatellite = shared.handleValueOrFn(overrides.isSatellite, new URL(request.url)) || IS_SATELLITE; const relativeOrAbsoluteProxyUrl = shared.handleValueOrFn( overrides?.proxyUrl, clerkRequest.clerkUrl, PROXY_URL ); const signInUrl = overrides.signInUrl || SIGN_IN_URL; const signUpUrl = overrides.signUpUrl || SIGN_UP_URL; const afterSignInUrl = overrides.afterSignInUrl || getPublicEnvVariables().afterSignInUrl; const afterSignUpUrl = overrides.afterSignUpUrl || getPublicEnvVariables().afterSignUpUrl; let proxyUrl; if (!!relativeOrAbsoluteProxyUrl && shared.isProxyUrlRelative(relativeOrAbsoluteProxyUrl)) { proxyUrl = new URL( relativeOrAbsoluteProxyUrl, clerkRequest.clerkUrl ).toString(); } else { proxyUrl = relativeOrAbsoluteProxyUrl; } if (!secretKey) { throw errorThrower.throw("Clerk: no secret key provided"); } if (isSatellite && !proxyUrl && !domain) { throw errorThrower.throw( "Clerk: satellite mode requires a proxy URL or domain" ); } if (isSatellite && !shared.isHttpOrHttps(signInUrl) && shared.isDevelopmentFromSecretKey(secretKey)) { throw errorThrower.throw( "Clerk: satellite mode requires a sign-in URL in production" ); } return { // used to append options that are not initialized from env ...overrides, secretKey, publishableKey, jwtKey, apiUrl, domain, isSatellite, proxyUrl, signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl }; }; // src/start/server/middleware.ts exports.clerkMiddleware = (options = {}) => { return async ({ request, locals }) => { try { const loadedOptions = loadOptions(request, options); const requestState = await authenticateRequest(request, loadedOptions); const state = getResponseClerkState(requestState, loadedOptions); locals.auth = internal.stripPrivateDataFromObject(requestState.toAuth()); locals.clerkInitialState = state.clerkInitialState; } catch (error) { if (error instanceof Response) { return error; } throw error; } }; };