psudo
psudo

Reputation: 1558

Redirect unauthenticated user nuxt js

I have the following code snippet in my middleware/auth.js

export default function ({ store, router }) {
  if (!store.state.admin.isAdmin) {
    router.push({ path: '/auth/login' })
  }
}

I want to redirect any unauthenticated users to login page. With the above code I'm getting error of:

Cannot read property 'push' of undefined

How do I access router properties in middleware so that I can redirect users ?

Upvotes: 0

Views: 719

Answers (1)

Ashkan
Ashkan

Reputation: 876

This is my middleware/auth.js:

export default function ({ app, redirect }) {
    if (process.client) {
        if (!app.$auth.loggedIn) {
            redirect('/login');
        }
    }
}

Upvotes: 1

Related Questions