userLogin.ts 584 B

12345678910111213141516171819202122
  1. import type { Router, LocationQueryRaw } from 'vue-router';
  2. import { useUserStore } from '@/store/modules/user';
  3. export default function setupUserLoginInfoGuard(router: Router) {
  4. router.beforeEach(async (to, from, next) => {
  5. const userStore = useUserStore();
  6. if (to.meta.requiresAuth && !userStore.isLoggedIn) {
  7. if (to.name == 'login') {
  8. next();
  9. return;
  10. } else {
  11. next({
  12. path: '/login',
  13. query: { redirect: to.fullPath } as LocationQueryRaw,
  14. });
  15. return;
  16. }
  17. } else {
  18. next();
  19. }
  20. });
  21. }