Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with NextAuth Authentication and URL Redirection in Nuxt.js #808

Closed
Eman031 opened this issue Jul 16, 2024 · 2 comments · Fixed by #809
Closed

Issue with NextAuth Authentication and URL Redirection in Nuxt.js #808

Eman031 opened this issue Jul 16, 2024 · 2 comments · Fixed by #809
Labels
bug A bug that needs to be resolved p2 Nice to have provider-authjs An issue with the authjs provider

Comments

@Eman031
Copy link

Eman031 commented Jul 16, 2024

Environment

  • Operating System: Darwin
  • Node Version: v18.17.0
  • Nuxt Version: 3.12.2
  • CLI Version: 3.12.0
  • Nitro Version: 2.9.6
  • Package Manager: npm@10.8.1
  • Builder: -
  • User Config: modules, typescript, runtimeConfig, auth, css
  • Runtime Modules: @nuxtjs/tailwindcss@6.12.0, @bg-dev/nuxt-naiveui@1.0.0-edge.2, @sidebase/nuxt-auth@0.7.2, @nuxt/icon@1.0.0, @pinia/nuxt@0.5.1, @nuxt/image@1.7.0, @nuxtjs/color-mode@3.4.2, @formkit/auto-animate/nuxt@0.8.2,
  • Build Modules: -

Reproduction

Not needed ...

Describe the bug

Hello everyone,

I'm encountering an issue with integrating NextAuth into my Nuxt.js application.

Context
I'm using the OAuth provider for authentication. When a user tries to access a protected route without being authenticated, they are redirected to the login page with callbackUrl and error query parameters.

Problem
After being redirected to the login page, the URL looks like this:

http://localhost:3000/login?callbackUrl=http://localhost:3000/dashboard&error=undefined

The error query parameter is set to undefined, and I would like to change this to a specific error message or remove it altogether.

When I do not use a custom login page, the URL looks like this:

http://localhost:3000/api/auth/signin?callbackUrl=%2Fdashboard

Additional context

nuxt.config.ts:
auth: { isEnabled: true, provider: { type: 'authjs' }, globalAppMiddleware: { isEnabled: true, } }

[...].ts
import .... export default NuxtAuthHandler({ adapter: PrismaAdapter(prisma), secret: process.env.AUTH_SECRET, providers: [ ... ], pages: { signIn: '/', }, callbacks: { session({ session, user }) { if (session.user) { session.user.id = user.id; } return session; }, }

Logs

No response

@Eman031 Eman031 added bug A bug that needs to be resolved pending An issue waiting for triage labels Jul 16, 2024
@zoey-kaiser
Copy link
Member

I assume this issue originates here. We wrap the error heading in String() and add a fallback to undefined if it does not exist. However, transfering error to a string, will always result it in being set, as wrapper undefined in string results in a string of undefined, which lines up with the path that gets generated!

Ill look into opening a PR for this 😊

@zoey-kaiser zoey-kaiser added p2 Nice to have provider-authjs An issue with the authjs provider and removed pending An issue waiting for triage labels Jul 16, 2024
@Eman031
Copy link
Author

Eman031 commented Jul 16, 2024

I assume this issue originates here. We wrap the error heading in String() and add a fallback to undefined if it does not exist. However, transfering error to a string, will always result it in being set, as wrapper undefined in string results in a string of undefined, which lines up with the path that gets generated!

Ill look into opening a PR for this 😊

All good now thank you!

@Eman031 Eman031 closed this as completed Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A bug that needs to be resolved p2 Nice to have provider-authjs An issue with the authjs provider
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants