安装Auth Module npm install @nuxtjs/auth
在nuxt.config.js配置文件中配置auth
modules: [
// Doc: https://axios.nuxtjs.org/usage
'@nuxtjs/axios',
'@nuxtjs/pwa',
'@nuxtjs/auth'
],
auth: {
strategies: {
local: {
endpoints: {
logout: { url: '/user/logout', method: 'post' },
user: {
url: '/user/currentUser',
method: 'get',
propertyName: 'data'
}
}
}
}
},
router: {
linkPrefetchedClass: 'nuxt-link-prefetched',
linkExactActiveClass: 'exact-active-link',
middleware: ['auth'], //配置中间件auth
prefetchLinks: false
}
在登录页面登录时
setUserToken(token: any) {
;(this as any).$auth.setUserToken(token).then(() => {
this.$router.push(
(this as any).$auth.user.roleType === 3 ? '/homework' : '/'
)
})
}