nuxt-auth icon indicating copy to clipboard operation
nuxt-auth copied to clipboard

Email Callback URL Not Set Correctly in Vercel Deployment

Open dandonovan78 opened this issue 2 years ago • 1 comments

Environment


  • Operating System: Darwin
  • Node Version: v21.2.0
  • Nuxt Version: 3.8.2
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.0
  • Package Manager: [email protected]
  • Builder: -
  • User Config: modules, ui, auth, devtools
  • Runtime Modules: @nuxt/[email protected], @sidebase/[email protected]
  • Build Modules: -

Reproduction

N/A

Describe the bug

I've added a simple EmailProvider to use a magic link to sign in. I have no special callback handling. I have a simple app with a protected page. When I try to access this page, I correctly am redirected to the sign-in page: https://.vercel.app/api/auth/signin?callbackUrl=%2Fcategories

I use the email provider to send the default email with a link. Locally, it works and includes the callback URL properly. In my Vercel deployment, instead of the expected callback, the URL is: https://.vercel.app/api/auth/callback/email?callbackUrl=https%3A%2F%2F.vercel.app%2Fsw.js&token=&email=

Then I get a 404 error on the sw.js page not found.

I'm not sure if it's related, but the documentation on AUTH_ORIGIN and baseUrl is a bit confusing. If I don't set an environment variable for AUTH_ORIGIN, the app crashes horribly. The docs state that baseUrl must include the full path, but if I include the "/api/auth" path portion along with my app URL in AUTH_ORIGIN, that causes the "/api/auth" portion to be duplicated. I've tried even hardcoding the "baseUrl" in my nuxt.config.ts file to include the /api/auth path, but it seems like it's overwritten by AUTH_ORIGIN?

nuxt.config.ts: auth: { isEnabled: true, baseUrl: 'https://.vercel.app/api/auth', provider: { type: 'authjs' }, globalAppMiddleware: { isEnabled: true }, },

export default NuxtAuthHandler({ secret: process.env.AUTH_SECRET, adapter: PrismaAdapter(prisma), providers: [ // @ts-expect-error You need to use .default here for it to work during SSR. May be fixed via Vite at some point GithubProvider.default({ clientId: process.env.GITHUB_AUTH_PROVIDER_CLIENT_ID, clientSecret: process.env.GITHUB_AUTH_PROVIDER_CLIENT_SECRET }), EmailProvider.default({ server: process.env.EMAIL_SERVER, from: process.env.EMAIL_FROM }) ], session: { strategy: 'jwt' } })

In vercel environment var: AUTH_ORIGIN=https://.vercel.app/

Additional context

No response

Logs

No response

dandonovan78 avatar Dec 05 '23 19:12 dandonovan78

Got this to work finally. Two things:

  1. I added typescript checking, which flagged that "baseUrl" should have been "baseURL". I set this to baseURL: AUTH_ORIGIN + "/api/auth"
  2. Took a while, but kind of figured out the sw.js part. I think this was a holdover from some experimental deployments with SSR set to false, then my browser had registered a service worker for that site and was making a GET request for it. Using Developer Tools (under Application) to stop and de-register the service worker made the 404 error go away, which allowed the callback URL generation to work correctly.

dandonovan78 avatar Dec 12 '23 04:12 dandonovan78