blue-salesclerk-13657
04/17/2025, 3:12 PMblue-salesclerk-13657
04/17/2025, 3:13 PMblue-salesclerk-13657
04/17/2025, 5:22 PMORY_SDK_URL
in the NextJs context. This worked to get the initial login page to render, but caused issues with any links that were included in the SSR'd login page as the URLs for these links would point to the in-cluster URL for kratos.
The fix was to instead use nextjs rewrites, like this:
env: {
NEXT_PUBLIC_ORY_SDK_URL: '/api/kratos',
ORY_SDK_URL: '<http://localhost:3000/api/kratos>', // use absolute URL server-side, which will be stable across deployments
},
async rewrites() {
return [
{
source: '/api/kratos/:path*',
destination: '<http://kratos-public.default.svc.cluster.local:4433/:path*>',
},
];
},
bland-eye-99092
04/22/2025, 7:57 PMblue-salesclerk-13657
04/25/2025, 1:56 PMexport function getOryElementsConfig() {
const kratosUrl = process.env.ORY_SDK_URL || process.env.NEXT_PUBLIC_ORY_SDK_URL || '';
const oryElementsConfig = enhanceOryConfig(baseConfig, kratosUrl);
return {
...oryElementsConfig,
sdk: {
...oryElementsConfig.sdk,
options: {
credentials: "include" as RequestCredentials
}
}
}
}
Is it expected for this to be required?narrow-park-87615
07/04/2025, 8:53 AMnarrow-park-87615
07/04/2025, 8:58 AMnarrow-park-87615
07/04/2025, 9:28 AMblue-salesclerk-13657
07/06/2025, 11:16 AMNEXT_PUBLIC_API_URL=<http://localhost:8080>
NEXT_PUBLIC_ORY_SDK_URL=<http://localhost:3000/.ory/kratos>
ORY_SDK_URL=<http://app-kratos-public.app.svc.cluster.local:4433> # we use k8s, but this would point to whatever URL your node service can resolve
Config helper fn:
export function getOryElementsConfig() {
const kratosUrl = process.env.NEXT_PUBLIC_ORY_SDK_URL || '';
const oryElementsConfig = enhanceOryConfig(baseConfig, kratosUrl);
return {
...oryElementsConfig,
sdk: {
...oryElementsConfig.sdk,
options: {
credentials: "include" as RequestCredentials
}
}
}
}
ory middleware.ts - copied exactly from the example repo, except we use the config fn above instead of the default config:
import { createOryMiddleware } from "@ory/nextjs/middleware"
import { getOryElementsConfig } from '@/lib/auth'
import {OryConfig} from "@ory/nextjs";
// This function can be marked `async` if using `await` inside
export const middleware = createOryMiddleware(getOryElementsConfig() as OryConfig)
// See "Matching Paths" below to learn more
export const config = {}
next config - the rewrite
is the critical bit here - I was unsuccesful at getting Ory to call the backend URL consistently via any combination of env vars, so giving a single URL that can be hit from the FE and rewritten by the node server to point to the backend URL got it working.
import type { NextConfig } from "next";
// Check if we're in a `npm run dev`
// Note: local deployments to k8s are considered 'production' in this context
const isDev = process.env.NODE_ENV === 'development';
const oryKratosRewriteDestination = isDev ?
`${process.env.ORY_SDK_URL}/:path*` :
'<http://app-kratos-public.app.svc.cluster.local:4433/:path*>'
// Parse the API URL from environment or use default
const apiUrl = process.env.NEXT_PUBLIC_API_URL || "<http://localhost:8080>";
const apiDomain = apiUrl.replace(/^https?:\/\//, "");
const nextConfig: NextConfig = {
images: {
domains: ["<http://images.unsplash.com|images.unsplash.com>", apiDomain, "localhost"],
},
devIndicators: false,
// Enable standalone output for Docker deployment
output: 'standalone',
async rewrites() {
return [
{
source: '/.ory/kratos/:path*',
destination: oryKratosRewriteDestination,
},
];
},
};
export default nextConfig;
ancient-magazine-81061
07/07/2025, 5:17 PMCannot find module '@ory/nextjs/middleware' or its corresponding type declarations.ts(2307)
Repo:
https://github.com/ory/docs/tree/master/code-examples/protect-page-login/nextjsblue-salesclerk-13657
07/07/2025, 5:29 PM"@ory/client": "^1.20.6",
"@ory/client-fetch": "^1.15.0-next.0",
"@ory/elements": "^0.5.3",
"@ory/elements-react": "^1.0.0-next.44",
"@ory/nextjs": "^1.0.0-next.5",
ancient-magazine-81061
07/07/2025, 6:39 PMnpm install
while not having the nextjs-app-router
folder as the root folder.
I appreciate you helping out regardless.ancient-magazine-81061
07/07/2025, 6:43 PM