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

WARN [Vue Router warn]: No match found for location with path "/api/auth/session"

Open perspolise opened this issue 1 year ago • 8 comments

I see this warning in console WARN [Vue Router warn]: No match found for location with path "/api/auth/session" and nuxt-auth doesn't work.

[...].ts

import GithubProvider from "@auth/core/providers/github"
import type { AuthConfig } from "@auth/core/types"
import { NuxtAuthHandler } from "#auth"

// The #auth virtual import comes from this module. You can use it on the client
// and server side, however not every export is universal. For example do not
// use sign-in and sign-out on the server side.

const runtimeConfig = useRuntimeConfig()

// Refer to Auth.js docs for more details
export const authOptions: AuthConfig = {
    secret: runtimeConfig.authJs.secret,
    providers: [
        GithubProvider({
            clientId: runtimeConfig.github.clientId,
            clientSecret: runtimeConfig.github.clientSecret
        })
    ]
}

export default NuxtAuthHandler(authOptions, runtimeConfig)

and nuxt.config.ts

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@hebilicious/authjs-nuxt"],
  authJs: {
    guestRedirectTo: "/redirected"
  },
  nitro: {
    routeRules: {
      "/": { ssr: true, prerender: true },
      "/private": { ssr: true, prerender: true }
    }
  },
  devtools: {
    enabled: true
  },
  experimental: {
    renderJsonPayloads: true
  },
  runtimeConfig: {
    authJs: { secret: "/OEjlRC2DK74ZEj5nl8qHNy+E6/JptnouIyHnANbBz0=" },
    github: {
      clientId: "",
      clientSecret: ""
    },
    public: {
      authJs: {
        baseUrl: "http://localhost:3000",
        verifyClientOnEveryRequest: true
      }
    }
  }
})

dependencies:

  "dependencies": {
    "@auth/core": "^0.34.1",
    "@hebilicious/authjs-nuxt": "^0.3.5",
    "nuxt": "^3.12.2",
    "vue": "latest"
  }

perspolise avatar Jun 29 '24 11:06 perspolise

There is a current workaround available, outlined in #158

mussingtonr avatar Jun 30 '24 18:06 mussingtonr

There is a current workaround available, outlined in #158

After change I see this error:

500
[GET] "/api/auth/session": 500

Stacktrace
async $fetchRaw2
ofetch/dist/shared/ofetch.37386b05.mjs:263:14
async $fetch2
ofetch/dist/shared/ofetch.37386b05.mjs:268:15
(anonymous)
@hebilicious/authjs-nuxt/dist/runtime/plugin.mjs:15:17
async setup
./D:/MAMP/htdocs/nuxt-auth/virtual:nuxt:D:/MAMP/htdocs/nuxt-auth/.nuxt/plugins/server.mjs:38:116
async Object.callAsync
unctx/dist/index.mjs:72:16
async applyPlugin
nuxt/dist/app/nuxt.js:144:25
async executePlugin
nuxt/dist/app/nuxt.js:181:9
async Module.applyPlugins
nuxt/dist/app/nuxt.js:195:5
async createNuxtAppServer
nuxt/dist/app/entry.js:24:7
async default
@nuxt/vite-builder/dist/runtime/vite-node.mjs:34:18

session.ts

import { type H3Event } from "h3"
import { getServerSession } from "#auth"
import { authOptions } from "~/server/api/auth/[...]"

export const getAuthSession = (event: H3Event) => getServerSession(event, authOptions)

perspolise avatar Jul 02 '24 08:07 perspolise

May I ask where you made the changes? Can you show me your updated [...].ts and Nuxt config?

mussingtonr avatar Jul 04 '24 06:07 mussingtonr

Resolved, by adding issuer.

@mussingtonr

It seems like @perspolise and I have similar problem and by changing the base path to /api/auth I get the same error as his

500
[GET] "/api/auth/session": 500

before it was

400 
[GET] "/api/auth/session": 400

this is the code for api/auth/[...].ts

import Auth0Provider from '@auth/core/providers/auth0'
import type { AuthConfig } from '@auth/core/types';
import { NuxtAuthHandler } from "#auth";

const runtimeConfig = useRuntimeConfig();

export const authOptions: AuthConfig = {
    // updated basePath
    basePath: '/api/auth',
    secret: runtimeConfig.authJs.secret,
    providers: [
        Auth0Provider({
            clientId: runtimeConfig.auth0.clientId,
            clientSecret: runtimeConfig.auth0.clientSecret
        })
    ],
} 

export default NuxtAuthHandler(authOptions, runtimeConfig)

cadiente-jomel avatar Jul 08 '24 10:07 cadiente-jomel

@cadiente-jomel : How do you resolved?!

perspolise avatar Jul 10 '24 07:07 perspolise

May I ask where you made the changes? Can you show me your updated [...].ts and Nuxt config?

[...].ts add basePath

import GithubProvider from "@auth/core/providers/github"
import type { AuthConfig } from "@auth/core/types"
import { NuxtAuthHandler } from "#auth"

const runtimeConfig = useRuntimeConfig()

export const authOptions: AuthConfig = {
  secret: runtimeConfig.authJs.secret,
  basePath: '/api/auth',
  theme: {
    logo: "https://nuxt.com/assets/design-kit/logo/icon-green.png"
  },
  providers: [
    GithubProvider({
      clientId: runtimeConfig.github.clientId,
      clientSecret: runtimeConfig.github.clientSecret
    })
  ]
}

export default NuxtAuthHandler(authOptions, runtimeConfig)

and nuxt.config.ts

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@hebilicious/authjs-nuxt"],
  authJs: {
    guestRedirectTo: "/redirected"
  },
  nitro: {
    routeRules: {
      "/": { ssr: true, prerender: true },
      "/private": { ssr: true, prerender: true }
    }
  },
  devtools: {
    enabled: true
  },
  experimental: {
    renderJsonPayloads: true
  },
  runtimeConfig: {
    authJs: { secret: "/OEjlRC2DK74ZEj5nl8qHNy+E6/JptnouIyHnANbBz0=" },
    github: {
      clientId: "",
      clientSecret: ""
    },
    public: {
      authJs: {
        baseUrl: "http://localhost:3000",
        verifyClientOnEveryRequest: true
      }
    }
  }
})

perspolise avatar Jul 10 '24 07:07 perspolise

May I ask where you made the changes? Can you show me your updated [...].ts and Nuxt config?

[...].ts add basePath

import GithubProvider from "@auth/core/providers/github"
import type { AuthConfig } from "@auth/core/types"
import { NuxtAuthHandler } from "#auth"

const runtimeConfig = useRuntimeConfig()

export const authOptions: AuthConfig = {
  secret: runtimeConfig.authJs.secret,
  basePath: '/api/auth',
  theme: {
    logo: "https://nuxt.com/assets/design-kit/logo/icon-green.png"
  },
  providers: [
    GithubProvider({
      clientId: runtimeConfig.github.clientId,
      clientSecret: runtimeConfig.github.clientSecret
    })
  ]
}

export default NuxtAuthHandler(authOptions, runtimeConfig)

and nuxt.config.ts

export default defineNuxtConfig({
  devtools: { enabled: true },
  modules: ["@hebilicious/authjs-nuxt"],
  authJs: {
    guestRedirectTo: "/redirected"
  },
  nitro: {
    routeRules: {
      "/": { ssr: true, prerender: true },
      "/private": { ssr: true, prerender: true }
    }
  },
  devtools: {
    enabled: true
  },
  experimental: {
    renderJsonPayloads: true
  },
  runtimeConfig: {
    authJs: { secret: "/OEjlRC2DK74ZEj5nl8qHNy+E6/JptnouIyHnANbBz0=" },
    github: {
      clientId: "",
      clientSecret: ""
    },
    public: {
      authJs: {
        baseUrl: "http://localhost:3000",
        verifyClientOnEveryRequest: true
      }
    }
  }
})

I can take a look at this later today, but, you should remove any front facing secrets you have and move them to an .env file (as well as not post those values on here and instead use a placeholder value or the process.env name :-) )

mussingtonr avatar Jul 10 '24 07:07 mussingtonr

@mussingtonr : right! Have you found any solution?

perspolise avatar Jul 12 '24 14:07 perspolise