Next-auth: How to set redirect_uri for preview deployments?

Created on 2 Aug 2020  路  5Comments  路  Source: nextauthjs/next-auth

Please refer to the documentation, the example project and existing issues before creating a new issue.

Your question
A clear and concise question.

Since vercel preview deployment url changes for every preview, how can we set oauth redirect uri for those? They also don't support wildcards. I'm trying to use google as an oauth provider

Documentation feedback
Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [x] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [ ] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [ ] Did not find the example project helpful
question

Most helpful comment

@iaincollins yes. I think having this feature will be something useful.
Here's my idea.

  • We have a single server which handle oauth stuff. For next-auth, we can consider the main deployment URL as that.
  • That's our NEXTAUTH_URL
  • All the login stuff are going through the above URL
  • When we got the callback URL, NEXTAUTH_URL will redirect in to another URL in the staging URL
  • Then it'll add all the related cookies etc
  • I think we need to find a way to enable some domains to prevent open redirects.

But this will be a killer feature, if we can do this inside next-auth.
There will be potential issues with my approach, but if we can do this, this will be an valuable contribution to the community.
And we can stand out from other auth libraries/solutions.

All 5 comments

Since vercel preview deployment url changes for every preview, how can we set oauth redirect uri for those? They also don't support wildcards. I'm trying to use google as an oauth provider

Most OAuth Providers do not support wildcards in callback URLs.

This is very awkward but not something we can do anything about.

Enabling an Email Provider or Credentials Provider on test instances can be helpful to work around this.

Note: If you enable the VERCEL_URL environment variable, NextAuth.js with pick that up if NEXTAUTH_URL is not set.

@iaincollins Thank you! will add an email provider

@iaincollins yes. I think having this feature will be something useful.
Here's my idea.

  • We have a single server which handle oauth stuff. For next-auth, we can consider the main deployment URL as that.
  • That's our NEXTAUTH_URL
  • All the login stuff are going through the above URL
  • When we got the callback URL, NEXTAUTH_URL will redirect in to another URL in the staging URL
  • Then it'll add all the related cookies etc
  • I think we need to find a way to enable some domains to prevent open redirects.

But this will be a killer feature, if we can do this inside next-auth.
There will be potential issues with my approach, but if we can do this, this will be an valuable contribution to the community.
And we can stand out from other auth libraries/solutions.

Note: If you enable the VERCEL_URL environment variable, NextAuth.js with pick that up if NEXTAUTH_URL is not set.

@iaincollins Would love your take on the following.

Sort of related to this issue (or perhaps just what I consider to be a broader issue) is that it seems NEXTAUTH_URL cannot be set within next.config.js like other env variables, specifically in the absence of build-time setting (.env file, Vercel UI settings for env)

I consider the API route to be more a runtime concern, so I'm confused why VERCEL_URL and localhost:3000 are the first and second defaults, with no extra runtime check for a NEXTAUTH_URL set later in the _race_ so to speak.

I could be totally missing the rationale here (maybe some quirks related to _serverless_ on Vercel), so apologies in advance if I'm off on the above 馃槄

Just saw this one - https://github.com/nextauthjs/next-auth/issues/497 - sorry if my comment above is a bit of a duplicate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryanditjia picture ryanditjia  路  3Comments

loonskai picture loonskai  路  3Comments

benoror picture benoror  路  3Comments

jimmiejackson414 picture jimmiejackson414  路  3Comments

simonbbyrne picture simonbbyrne  路  3Comments