ory / ory/elements

Bug: guessPotentiallyProxiedOrySdkUrl Returns Wrong SDK Url for Staging (nextJS)

Open
#572 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
187
Forks
80
PR merge metrics
No merged PRs in 30d

Description

Preflight checklist
Ory Network Project

No response

Describe the bug

I have run into an issue with the ory/nextjs package, specifically the getLoginFlow function:

https://github.com/ory/elements/blob/e159960474eb1351bea86afab6d930f41b4acae6/packages/nextjs/src/app/login.ts#L47

We call this function when a user navigates to the login page of our site.

Under the hood it uses this function to determine the ory SDK Url:
https://github.com/ory/elements/blob/e159960474eb1351bea86afab6d930f41b4acae6/packages/elements-react/src/client/config.ts#L46

In Vercel we have a production and a staging environment. This works fine for our production environment. But in our staging environment the user is sent to the wrong link, e.g. xyz.vercel.app because the logic in this function causes the us to hit this line which sets our sdk url as the incorrect address:

    if (!isProduction() && process.env.VERCEL_URL) {
      return `https://${process.env.VERCEL_URL}`.replace(/\/$/, "")
    }

But we need the request to be directed to the address we set with this variable, even in staging: NEXT_PUBLIC_ORY_SDK_URL

Always very grateful for your help and continued support 🙏

Reproducing the bug
  1. Clone this example project https://github.com/ory/elements/tree/main/examples/nextjs-app-router
  2. Publish to Github
  3. Link the project in vercel and create a project from main branch.
  4. GIve the project an additional custom environment called "staging".
  5. Set the environment variable for each environment NEXT_PUBLIC_ORY_SDK_URL as any value.
  6. The login flow will work for production environment but fail for staging environment, with users being directed to xyz.vercel.app when they initiate a login sequence
Relevant log output

Relevant configuration

Version

1.0.0-rc.0

On which operating system are you observing this issue?

None

In which environment are you deploying?

None

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/elements-react/src/client/config.ts, especially guessPotentiallyProxiedOrySdkUrl, and trace its use from packages/nextjs/src/app/login.ts. Reproduce the staging deployment described with examples/nextjs-app-router and inspect how NEXT_PUBLIC_ORY_SDK_URL and VERCEL_URL are selected. Done means staging login flows use the configured SDK URL while production behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.