Middleware using cookie nuxt

时间:2018-11-13 07:51:23

标签: vue.js routes middleware nuxt.js

I just setup middleware by checking cookie in nuxt but it's give me blank error, nothing error in console

enter image description here

this is my middleware authenticate.js

export default function({ store, redirect }) {
  const Cookie = process.client ? require('js-cookie') : undefined
  // If the user is not authenticated
  let token = Cookie.get('token')
  const userIsLoggedIn = token != null //cookie ada
  const userNeedLogin = token == null //cookie gak ada
  if (userNeedLogin) {
    return redirect('/login')
  }
  if (userIsLoggedIn) {
    return redirect('/dashboard')
  }
}

and this is my nuxt.config.js

router: { 
    middleware: 'authenticate'
  }

it should redirect to page login when cookies is null but if cookie exist it redirect goes to dashboard, do i miss something ?

0 个答案:

没有答案