nuxt-alt / auth

An alternative module to @nuxtjs/auth
https://nuxt-alt-auth.vercel.app/
MIT License
113 stars 20 forks source link

Error useRoute at middleware #53

Closed ftrsoft closed 1 year ago

ftrsoft commented 1 year ago

Environment

Nuxt project info: 21:46:47


Nuxt Config

// https://nuxt.com/docs/api/configuration/nuxt-config export default defineNuxtConfig({ modules: [ 'nuxt-quasar-ui', // '@nuxt-alt/http', '@nuxt-alt/auth', '@pinia/nuxt', ], quasar: { iconSet: 'mdi-v7', plugins: ['Notify'], extras: { font: 'roboto-font', fontIcons: ['material-icons', 'mdi-v7'], }, }, auth: { strategies: { local: { scheme: 'refresh', localStorage: { prefix: 'auth.' }, token: { prefix: 'access_token.', property: 'results.access', maxAge: 60 5, global: true, }, refreshToken: { prefix: 'refresh_token.', property: 'results.refresh', data: 'refresh', maxAge: 60 60 24 15 }, user: { property: 'results.user', autoFetch: true }, endpoints: { login: {url: '/api/token/', method: 'post'}, refresh: {url: '/api/token/refresh/', method: 'post'}, user: {url: '/api/v1/user/', method: 'get'}, logout: {url: '/api/v1/user_logout/', method: 'post'} }, watchLoggedIn: true, redirectStrategy: 'query', redirect: { login: '/login', logout: '/', callback: '/', home: '/' } } }, pinia: { persist: true, } }, http: { baseURL: 'http://localhost:8000/', browserBaseURL: 'http://localhost:8000/', }, runtimeConfig: { app: { // baseURL: 'http://localhost:8000/', } } })

Reproduction

no

Describe the bug

When use auth middleware there are error at concole: [nuxt] Calling useRoute within middleware may lead to misleading results. Instead, use the (to, from) arguments passed to the middleware to access the new and old routes. useRoute @ router.js:12 redirect @ auth.mjs:262 (anonymous) @ middleware.mjs?v=246077ed:36 fn @ nuxt.js:203 callWithNuxt @ nuxt.js:208 (anonymous) @ router.js?v=246077ed:113 await in (anonymous) (async) (anonymous) @ vue-router.js:1331 (anonymous) @ vue-router.js:1309 (anonymous) @ vue-router.js:2588 Promise.then (async) (anonymous) @ vue-router.js:2588 runGuardQueue @ vue-router.js:2588 (anonymous) @ vue-router.js:2338 Promise.then (async) navigate @ vue-router.js:2332 pushWithRedirect @ vue-router.js:2270 push @ vue-router.js:2206 replace @ vue-router.js:2209 (anonymous) @ router.js?v=246077ed:152 _function @ index.mjs?v=246077ed:133 (anonymous) @ index.mjs?v=246077ed:48 (anonymous) @ index.mjs?v=246077ed:48 app:created (async) serialTaskCaller @ index.mjs?v=246077ed:46 callHookWith @ index.mjs?v=246077ed:198 callHook @ index.mjs?v=246077ed:187 initApp @ entry.js:47 await in initApp (async) (anonymous) @ entry.js:57

Additional context

(anonymous) @ middleware.mjs?v=246077ed:36 it is string near

  } else if (!pageIsInGuestMode && (!callback || !insidePage(callback))) {
    return ctx.$auth.redirect("login", to);
  }

Logs

No response

ftrsoft commented 1 year ago

maybe need use navigateTo function instead ctx.$auth.redirect("login", to);

Denoder commented 1 year ago

If you look at the readme, there is an option to change to the navigateTo function