Has anyone else used Ory with the <nextjs-app-rout...
# ory-network
s
Has anyone else used Ory with the nextjs-app-router on Vercel? I am experiencing this issue where the login flow redirects me to a commit-specific Vercel URL when I kick off the auth flow. (Continuing from https://ory-community.slack.com/archives/C02MR4DEEGH/p1744216655875899)
I’m guessing that this behaviour comes from this code: https://github.com/ory/elements/blob/733e2bd6e9841a89f14da0910d2f363d4edf05c1/packages/nextjs/src/utils/sdk.ts#L42-L44 I guess the
VERCEL_URL
is set to the auto-generated URL?
it seems like we can’t override the
NEXT_PUBLIC_VERCEL_URL
, meaning that we can’t use anything other than the default deployment URL (
<http://my-site-7q03y4pi5.vercel.app|my-site-7q03y4pi5.vercel.app>
)
b
Is this for use in production? In production you typically don't need the middleware, because Ory's URL would be on the same domain (can still be a different subdomain). So Cookies & CORS are not an issue, as they are in development/preview envs.
s
@bland-eye-99092 no, not for production.
URL would be on the same domain (can still be a different subdomain). So Cookies & CORS are not an issue
Isn’t a different subdomain still a separate origin?
b
Yes, but you can configure both CORS and especially Cookies easily to work on both - if they are on the same second-level domain. With the removal of third party cookies, we disallow issuing cookies on different second level domains - hence the distinction. And the middleware makes working with local deployments much easier, than having to deal with CORS on those local envs. Am I understanding correctly, that you have a preview deployment with a custom domain?
s
Right, we have https://beta.source.coop/. However, if you begin that login flow in the upper right hand corner, we get redirected to the the semi-random deployment URL
admittedly, this is not a HUGE deal, however it would be preferable to test/demo the auth flow in a production-like manner
thruthfully, I would prefer to not even handle auth in my application and instead redirect to Ory, however for reasons described below, I wasn’t able to do so. https://ory-community.slack.com/archives/C02MR4DEEGH/p1744216655875899
b
do you have a custom domain set up? If you have a custom domain, the CORS issue goes away, AFAIK.
s
I can take another crack at that but I believe I went down that road…
Is there an idiomatic reference to a NextJS application or general ReactJS application that demonstrates such a flow that you could point me to? @bland-eye-99092
I was doing a lot of dancing around with Ory Tunnel before
b
We're in the process of streamlining all of that. But if you're on nextjs, you won't need the tunnel anymore, as the middleware handles all of that. We're not quite there yet, though. So the edges are still pretty rough. We really appreciate your rigorous feedback though! As for the original issue: we recommend using CNAMEs for production and production like envs. However, you could be running into this: https://github.com/ory/elements/blob/83015ee711a0c93685f7db89aebcb773482729b8/packages/nextjs/src/utils/sdk.ts#L36-L50, It would be interesting to know what the value of
VERCEL_ENV
is for your deployment.
s
Your link isn’t working, but I assume you’re referencing the code I mentioned here: https://ory-community.slack.com/archives/C02MR4DEEGH/p1745252111240049?thread_ts=1745251949.679419&amp;cid=C02MR4DEEGH
s
do you have time / interest in a quick pair call?
it would be great to get some advice on the quickest way to swap out the elements toolkit for ory-hosted auth
b
Ah yea, you already linked that - sorry. I'll have to read through the linked issue to get a better understanding, but it looks like we might need to add special handling for
VERCEL_ENV=preview
, as that should behave similar to
production
, IMO. Your end goal is to move the hosted account experience?
s
Your end goal is to move the hosted account experience?
I think that would be the best, we don’t have super strict branding needs that couldn’t be addressed with Ory hosted. We actually do use the Ory hosted setup in production today (https://auth.source.coop/) but have basically rewritten the frontend and I’m trying to understand what is necessary from the legacy-style of using Ory to the modern-style of using Ory. ie, I’m loving the middleware rather than the tunnel, but I’m not sure what the ideal auth flow is for a NextJS app with SSR. Some Ory docs reference cookies, others reference the classic Client Authorization flow which I assumes implies JWTs. I’m having some issues understanding which auth workflow would be ideal for a modern NextJS app that would fit in with Ory’s best-practices in the upcoming future
b
We're still looking into how to solve this. You can follow along here: https://github.com/ory/elements/pull/484 We most often recommend Cookie based auth, because of the drawbacks (complexity, handling tokens, etc.) of oauth2. If you don't have a specific use-case for oauth2, use sessions. Ory Elements provides a more hands-on experience, but also greater flexibility than the built-in UIs. You mentioned theming needs that could not be met, what were those?