Denoder / nuxt-module-alternatives

Alternative modules to use while waiting for Nuxt 3 Compatibility
MIT License
144 stars 14 forks source link

Middleware 500 Server Error because $auth not initiated yet #84

Closed deanmikan closed 2 years ago

deanmikan commented 2 years ago

Getting a 500 server error when trying to access nuxtApp.$auth in middleware.

Only started occurring after upgrading to the latest. Was previously on versions nuxt rc.8 and nuxtjs-alt/auth 1.2.4 but upgraded to nuxt rc.9 and nuxtjs-alt/auth 2.0.14 and this error started occurring.

image

export default defineNuxtRouteMiddleware(async (to, from) => {
  const app = useNuxtApp();

  // If user is trying to access a restricted page without authentication redirect to login page
  if (!app.$auth.loggedIn) {
    return navigateTo({
      name: "login",
      query: to.query,
    });
  }
});
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.