permission.js 444 B

123456789101112131415161718192021
  1. import router from './router'
  2. import { getAccessToken, removeAll } from '@/utils/auth'
  3. router.beforeEach((to, from, next) => {
  4. const needAuth = to.meta.needAuth
  5. const token = getAccessToken()
  6. if (token != null) {
  7. if (to.path === '/login' || to.path === '/register') {
  8. next({ path: '/' })
  9. } else {
  10. next()
  11. }
  12. } else {
  13. removeAll()
  14. if (needAuth) {
  15. next('/login')
  16. } else {
  17. next()
  18. }
  19. }
  20. })