@ory/nextjs
Version:
This library was generated with [Nx](https://nx.dev).
227 lines (220 loc) • 7.35 kB
JavaScript
;
var clientFetch = require('@ory/client-fetch');
var react = require('react');
var router = require('next/router');
var navigation = require('next/navigation');
require('set-cookie-parser');
require('cookie');
require('psl');
// src/utils/sdk.ts
function orySdkUrl() {
let baseUrl;
if (process.env["NEXT_PUBLIC_ORY_SDK_URL"]) {
baseUrl = process.env["NEXT_PUBLIC_ORY_SDK_URL"];
}
if (!baseUrl) {
throw new Error(
"You need to set environment variable `NEXT_PUBLIC_ORY_SDK_URL` to your Ory Network SDK URL."
);
}
return baseUrl.replace(/\/$/, "");
}
function isProduction() {
return ["production", "prod"].indexOf(
process.env["VERCEL_ENV"] || process.env["NODE_ENV"] || ""
) > -1;
}
function guessPotentiallyProxiedOrySdkUrl(options) {
if (isProduction()) {
return orySdkUrl();
}
if (process.env["VERCEL_ENV"]) {
if (!isProduction() && process.env["VERCEL_URL"]) {
return `https://${process.env["VERCEL_URL"]}`.replace(/\/$/, "");
}
if (process.env["__NEXT_PRIVATE_ORIGIN"]) {
return process.env["__NEXT_PRIVATE_ORIGIN"].replace(/\/$/, "");
}
}
if (typeof window !== "undefined") {
return window.location.origin;
}
if (options?.knownProxiedUrl) {
return options.knownProxiedUrl;
}
const final = orySdkUrl();
console.warn(
`Unable to determine a suitable SDK URL for setting up the Next.js integration of Ory Elements. Will proceed using default Ory SDK URL "${final}". This is likely not what you want for local development and your authentication and login may not work.`
);
return final;
}
// src/pages/client.ts
var clientSideFrontendClient = () => new clientFetch.FrontendApi(
new clientFetch.Configuration({
headers: {
Accept: "application/json"
},
credentials: "include",
basePath: guessPotentiallyProxiedOrySdkUrl({
knownProxiedUrl: window.location.origin
})
})
);
function onValidationError(value) {
return value;
}
var toBrowserEndpointRedirect = (params, flowType) => guessPotentiallyProxiedOrySdkUrl({
knownProxiedUrl: window.location.origin
}) + "/self-service/" + flowType.toString() + "/browser?" + new URLSearchParams(params).toString();
var handleRestartFlow = (searchParams, flowType) => () => {
window.location.assign(toBrowserEndpointRedirect(searchParams, flowType));
};
function useOnRedirect() {
const router$1 = router.useRouter();
return (url, external) => {
if (external) {
window.location.assign(url);
} else {
void router$1.push(url);
}
};
}
// src/utils/rewrite.ts
function rewriteJsonResponse(obj, proxyUrl) {
return Object.fromEntries(
Object.entries(obj).filter(([_, value]) => value !== void 0).map(([key, value]) => {
if (Array.isArray(value)) {
return [
key,
value.map((item) => {
if (typeof item === "object" && item !== null) {
return rewriteJsonResponse(item, proxyUrl);
} else if (typeof item === "string" && proxyUrl) {
return item.replaceAll(orySdkUrl(), proxyUrl);
}
return item;
}).filter((item) => item !== void 0)
];
} else if (typeof value === "object" && value !== null) {
return [key, rewriteJsonResponse(value, proxyUrl)];
} else if (typeof value === "string" && proxyUrl) {
return [key, value.replaceAll(orySdkUrl(), proxyUrl)];
}
return [key, value];
})
);
}
// src/utils/utils.ts
function toValue(res) {
return res.value().then((v) => rewriteJsonResponse(v));
}
// src/pages/flow.ts
function createUseFlowFactory(flowType, createFlow, getFlow) {
return () => {
const [flow, setFlow] = react.useState();
const router$1 = router.useRouter();
const searchParams = navigation.useSearchParams();
const onRestartFlow = handleRestartFlow(searchParams, flowType);
const onRedirect = useOnRedirect();
const errorHandler = clientFetch.handleFlowError({
onValidationError,
onRestartFlow,
onRedirect
});
const handleSetFlow = async (flow2) => {
setFlow(flow2);
await router$1.replace({
query: { flow: flow2.id }
});
return;
};
react.useEffect(() => {
const id = searchParams.get("flow");
if (!router$1.isReady || flow !== void 0) {
return;
}
if (!id) {
createFlow(searchParams).then(toValue).then(handleSetFlow).catch(errorHandler);
return;
}
getFlow(id).then(toValue).then(handleSetFlow).catch(errorHandler);
}, [searchParams, router$1, router$1.isReady, flow]);
return flow;
};
}
var useRegistrationFlow = createUseFlowFactory(
clientFetch.FlowType.Registration,
(params) => {
return clientSideFrontendClient().createBrowserRegistrationFlowRaw({
returnTo: params.get("return_to") ?? void 0,
loginChallenge: params.get("registration_challenge") ?? void 0,
afterVerificationReturnTo: params.get("after_verification_return_to") ?? void 0,
organization: params.get("organization") ?? void 0
});
},
(id) => clientSideFrontendClient().getRegistrationFlowRaw({ id })
);
var useVerificationFlow = createUseFlowFactory(
clientFetch.FlowType.Verification,
(params) => {
return clientSideFrontendClient().createBrowserVerificationFlowRaw({
returnTo: params.get("return_to") ?? void 0
});
},
(id) => clientSideFrontendClient().getVerificationFlowRaw({ id })
);
var useRecoveryFlow = createUseFlowFactory(
clientFetch.FlowType.Recovery,
(params) => {
return clientSideFrontendClient().createBrowserRecoveryFlowRaw({
returnTo: params.get("return_to") ?? void 0
});
},
(id) => clientSideFrontendClient().getRecoveryFlowRaw({ id })
);
var useLoginFlow = createUseFlowFactory(
clientFetch.FlowType.Login,
(params) => {
return clientSideFrontendClient().createBrowserLoginFlowRaw({
refresh: params.get("refresh") === "true",
aal: params.get("aal") ?? void 0,
returnTo: params.get("return_to") ?? void 0,
cookie: params.get("cookie") ?? void 0,
loginChallenge: params.get("login_challenge") ?? void 0,
organization: params.get("organization") ?? void 0,
via: params.get("via") ?? void 0
});
},
(id) => clientSideFrontendClient().getLoginFlowRaw({ id })
);
var useSettingsFlow = createUseFlowFactory(
clientFetch.FlowType.Settings,
(params) => {
return clientSideFrontendClient().createBrowserSettingsFlowRaw({
returnTo: params.get("return_to") ?? void 0,
cookie: params.get("cookie") ?? void 0
});
},
(id) => clientSideFrontendClient().getSettingsFlowRaw({ id })
);
function useLogoutFlow() {
const [flow, setFlow] = react.useState(void 0);
const createFlow = async () => {
const flow2 = await clientSideFrontendClient().createBrowserLogoutFlow({});
setFlow(flow2);
};
react.useEffect(() => {
if (!flow) {
void createFlow();
}
}, []);
return flow;
}
exports.useLoginFlow = useLoginFlow;
exports.useLogoutFlow = useLogoutFlow;
exports.useRecoveryFlow = useRecoveryFlow;
exports.useRegistrationFlow = useRegistrationFlow;
exports.useSettingsFlow = useSettingsFlow;
exports.useVerificationFlow = useVerificationFlow;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map