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

Invalid Url upgrading to 0.6.2

Open agracia-foticos opened this issue 1 year ago • 25 comments

Environment

  • Operating System: Linux
  • Node Version: v18.15.0
  • Nuxt Version: 3.8.2
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.0
  • Package Manager: [email protected]
  • Builder: -
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

No response

Describe the bug

I'm upgrading from 0.5.0 to 0.6.2

this is my nuxt.config.ts auth: { // @ts-ignore (isEnabled boolean type) //BE CAREFULL, With visual studio debugger isEnabled can't be false, fails... issue opened isEnabled : process.env.NUXT_AUTH_ENABLED=="true", //all environment variables are strings baseURL :http://localhost:3000/api/auth, // The origin is set to the development origin. Change this when deploying to production by setting originin this config before build-time or by exportingAUTH_ORIGINby runningexport AUTH_ORIGIN=... // Whether to add a global authentication middleware that will protect all pages without exclusion globalAppMiddleware : false, provider : { type: 'authjs' } },

My NuxtAuthHandler are in : image

The error is: { "url": "/api/auth/callback/credentials", "statusCode": 500, "statusMessage": "", "message": "Invalid URL", "stack": "<pre><span class=\"stack internal\">at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)</span>\n<span class=\"stack internal\">at new NodeError (node:internal/errors:399:5)</span>\n<span class=\"stack internal\">at URL.onParseError (node:internal/url:566:9)</span>\n<span class=\"stack internal\">at new URL (node:internal/url:646:5)</span>\n<span class=\"stack internal\">at Object.redirect (./node_modules/next-auth/core/lib/default-callbacks.js:16:65)</span>\n<span class=\"stack internal\">at createCallbackUrl (./node_modules/next-auth/core/lib/callback-url.js:20:35)</span>\n<span class=\"stack internal\">at init (./node_modules/next-auth/core/init.js:131:48)</span>\n<span class=\"stack internal\">at AuthHandler (./node_modules/next-auth/core/index.js:131:28)</span>\n<span class=\"stack internal\">at process.processTicksAndRejections (node:internal/process/task_queues:95:5)</span>\n<span class=\"stack internal\">at <anonymous> (./node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs:88:24)</span>\n<span class=\"stack internal\">at async Object.handler (./node_modules/h3/dist/index.mjs:1675:19)</span>\n<span class=\"stack internal\">at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1885:7)</span></pre>" }

Any idea that whats happend?

Additional context

No response

Logs

No response

agracia-foticos avatar Nov 30 '23 12:11 agracia-foticos

Any solution??

agracia-foticos avatar Dec 13 '23 14:12 agracia-foticos

Any notice?

agracia-foticos avatar Dec 15 '23 08:12 agracia-foticos

image i try a lot of diferent code, but in all versions of my code raises "invalid url", please i need help!!!

agracia-foticos avatar Dec 15 '23 08:12 agracia-foticos

I faced a similar issue after upgrading nuxt-auth from 0.6.2 to 0.6.3 and next-auth from 4.21.1 to 4.22.5. I'm using a custom provider in my case, I had to set NEXTAUTH_URL environment variable, same value than AUTH_ORIGIN one.

davidlemaitre avatar Dec 15 '23 11:12 davidlemaitre

I add NEXTAUTH_URL="http://localhost:3000/api/auth" to my .env but i have the same error: "invalid url"

agracia-foticos avatar Dec 15 '23 11:12 agracia-foticos

Try NEXTAUTH_URL=http://localhost:3000 and AUTH_ORIGIN=http://localhost:3000 And my NuxtAuthHandler is located to <project-root>/server/api/auth/[...].ts

davidlemaitre avatar Dec 15 '23 12:12 davidlemaitre

Try NEXTAUTH_URL=http://localhost:3000 and AUTH_ORIGIN=http://localhost:3000 And my NuxtAuthHandler is located to <project-root>/server/api/auth/[...].ts

.env image

/server/api/auth/[...].ts image

nuxt.config.ts image

Same error :(

agracia-foticos avatar Dec 15 '23 13:12 agracia-foticos

Remove the space after equals sign for the NUXT_BASE_URL environment variable in your .env file

davidlemaitre avatar Dec 15 '23 13:12 davidlemaitre

Remove the space after equals sign for the NUXT_BASE_URL environment variable in your .env file

image

There isnt spaces in NUXT_BASE_URL :(

agracia-foticos avatar Dec 15 '23 13:12 agracia-foticos

Try baseURL: process.env.NUXT_BASE_URL in the nuxt.config.ts

davidlemaitre avatar Dec 15 '23 13:12 davidlemaitre

Try baseURL: process.env.NUXT_BASE_URL in the nuxt.config.ts

It's the same! image

Not works! :(

agracia-foticos avatar Dec 15 '23 13:12 agracia-foticos

Maybe the environment variables are not available in your setup. Try to use hardcoded values for both isEnabled and baseURL settings. Or remove baseURL config from nuxt.config.ts file, the default is http://localhost:3000/api/auth like you want in development.

davidlemaitre avatar Dec 15 '23 14:12 davidlemaitre

Hi @agracia-foticos,

As @davidlemaitre suspects, I think that somewhere a url is not being set correctly. Therefore I would recommend the following to try and debug this problem:

  • remove all environment variables relating to auth
  • Remove baseURL from the nuxt.config (in development, NuxtAuth can automatically determine your origin)
  • If this does not work, please hard code baseURL in your nuxt.config to localhost:3000.

I also wanted to remind you that setting baseURL in the nuxt config is for injecting the environment variable at build time and setting it via AUTH_ORIGIN is for injecting it runtime. You do not need to set both, one is enough!

zoey-kaiser avatar Dec 18 '23 07:12 zoey-kaiser

Hi @agracia-foticos,

As @davidlemaitre suspects, I think that somewhere a url is not being set correctly. Therefore I would recommend the following to try and debug this problem:

  • remove all environment variables relating to auth
  • Remove baseURL from the nuxt.config (in development, NuxtAuth can automatically determine your origin)
  • If this does not work, please hard code baseURL in your nuxt.config to localhost:3000.

I also wanted to remind you that setting baseURL in the nuxt config is for injecting the environment variable at build time and setting it via AUTH_ORIGIN is for injecting it runtime. You do not need to set both, one is enough!

nuxt.config.ts image

the only key in .env its NUXT_AUTH_SECRET

image

same issue [nuxt] [request error] [unhandled] [500] Invalid URL at __node_internal_captureLargerStackTrace (node:internal/errors:496:5) at new NodeError (node:internal/errors:405:5) at new URL (node:internal/url:778:13) at Object.redirect (./node_modules/next-auth/core/lib/default-callbacks.js:16:65) at createCallbackUrl (./node_modules/next-auth/core/lib/callback-url.js:20:35) at init (./node_modules/next-auth/core/init.js:131:48) at AuthHandler (./node_modules/next-auth/core/index.js:131:28) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at <anonymous> (./node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs:88:24) at async Object.handler (./node_modules/h3/dist/index.mjs:1675:19) at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:1885:7)

agracia-foticos avatar Dec 18 '23 08:12 agracia-foticos

@agracia-foticos Could you please show me your NuxtAuthHandler (the file inside /api/auth/[...].ts). I feel like there may be an issue in there 😄

Otherwise, could you create a small reproduction of the issue? I think this would greatly help me, find the issue and expatiate the process!

zoey-kaiser avatar Dec 18 '23 11:12 zoey-kaiser

@agracia-foticos Could you please show me your NuxtAuthHandler (the file inside /api/auth/[...].ts). I feel like there may be an issue in there 😄

Otherwise, could you create a small reproduction of the issue? I think this would greatly help me, find the issue and expatiate the process!

The NuxtAuthHandler runs in 0.5.2 version... i think there isnt the problem here.

i try to give you a small reproduction, but it's going to be complicated

agracia-foticos avatar Dec 19 '23 06:12 agracia-foticos

The NuxtAuthHandler runs in 0.5.2 version... i think there isnt the problem here.

I saw that the error originated from the section in nuxt-auth concering itself with the NuxtAuthHandler, which is why I wanted to verify, that the problem would not occur from there!

zoey-kaiser avatar Dec 19 '23 07:12 zoey-kaiser

The NuxtAuthHandler runs in 0.5.2 version... i think there isnt the problem here.

I saw that the error originated from the section in nuxt-auth concering itself with the NuxtAuthHandler, which is why I wanted to verify, that the problem would not occur from there!

NuxtAuthHandler without our auth authentication... i delete the code into 'async authorize"

import CredentialsProvider from 'next-auth/providers/credentials'
import { NuxtAuthHandler } from '#auth'
import { GOOGLE_LOGIN, USER_PASSWORD_LOGIN } from '@/constants/wanapix'
import type { WebUser } from '@/types/fapi/WebUser'

export default NuxtAuthHandler({
 
  pages: {
    
    signIn: `/login`, //`/${LOGIN_SLUG}`
  },
  secret: <string>useRuntimeConfig().authSecret,
  providers: [
  
    CredentialsProvider.default({
      // The name to display on the sign in form (e.g. 'Sign in with...')
      name: 'Credentials',
      // The credentials is used to generate a suitable form on the sign in page.
      // You can specify whatever fields you are expecting to be submitted.
      // e.g. domain, username, password, 2FA token, etc.
      // You can pass any HTML attribute to the <input> tag through the object.
      async authorize (credentials: WebUser) {
       
     

                 
      }
    }),
  ],
  callbacks: {
    // Callback when the JWT is created / updated, see https://next-auth.js.org/configuration/callbacks#jwt-callback
    jwt: async ({token, user}) => {
      const isSignIn = user ? true : false;
      if (isSignIn) {
        (token as any) = user
      }
      return Promise.resolve(token);
    },
    // Callback whenever session is checked, see https://next-auth.js.org/configuration/callbacks#session-callback
    session: async ({session, token}) => {
      (session as any) = token;
      return Promise.resolve(session);
    },
  },
})

agracia-foticos avatar Dec 19 '23 10:12 agracia-foticos

Any idea?

agracia-foticos avatar Dec 27 '23 14:12 agracia-foticos

I have upgraded the version to 0.6.3 but we have the same error :(

agracia-foticos avatar Dec 28 '23 09:12 agracia-foticos

@agracia-foticos, does it work on local development, if you set no Environment variable at all? When starting the project in development mode, it should be able to auto get the URL. This would show if:

  • The issue lies within your environment variables
  • In something else

Other then that, there isn't much I can directly find inside the files and information you provided. If you could create a new empty project and try to replicate the issue, then I could look further into it. I myself am using NuxtAuth 0.6.3 in my own projects and have not encountered this issue.

zoey-kaiser avatar Jan 02 '24 11:01 zoey-kaiser

Looks like I had a similar error

When I tried signIn('google') it redirected me to the login page with invalid url error in the console, although before it was opening google auth page right away

removing line globalAppMiddleware: false

helped me

ivryb avatar Jan 04 '24 23:01 ivryb

I remove globalAppMiddleware:false and works fine!!!!

agracia-foticos avatar Jan 08 '24 06:01 agracia-foticos

Amazing! Seems to be an issue in the redirecting. Ill look into this and see why its caused!

zoey-kaiser avatar Jan 08 '24 17:01 zoey-kaiser

Same issue on 0.6.7

[nuxt] [request error] [unhandled] [500] Invalid URL at new URL (node:internal/url:787:36)
at ./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/lib/providers.js:55:19
at Array.reduce ()
at normalizeOAuthOptions (./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/lib/providers.js:51:51)
at ./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/lib/providers.js:25:37
at Array.map ()
at parseProviders (./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/lib/providers.js:15:38)
at init (./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/init.js:56:30)
at AuthHandler (./node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next-auth/core/index.js:138:28)

magnetic5355 avatar Feb 23 '24 06:02 magnetic5355

Closed due to an old version. If anyone is still experiencing this issue, please check if it still exists in 0.8.0 and that the module has been setup using the new docs: https://auth.sidebase.io

zoey-kaiser avatar Jul 13 '24 10:07 zoey-kaiser

Closed due to an old version. If anyone is still experiencing this issue, please check if it still exists in 0.8.0 and that the module has been setup using the new docs: https://auth.sidebase.io

I'm afraid it's still an issue in @sidebase/nuxt-auth:0.9.3 + next-auth:4.21.1.

I got "[500] Invalid URL" while calling signIn function. Removing globalAppMiddleware: false, from nuxt.config.ts solved the issue. Setting it to true also solves the issue (but it's not what I wanted).

jerryfromearth avatar Sep 30 '24 22:09 jerryfromearth