permission.js 468 B

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