teacherGuard.js 596 B

1234567891011121314
  1. // Teacher role route guard for Vue Router
  2. // Import this guard in your router entry (e.g., src/router/index.js) and call `router.beforeEach(teacherGuard)`
  3. export default function teacherGuard(to, from, next) {
  4. // Assume user role is stored in localStorage after login
  5. const role = localStorage.getItem('role');
  6. // List of routes that require teacher role (prefix /guide)
  7. const teacherOnly = to.path.startsWith('/guide');
  8. if (teacherOnly && role !== 'teacher') {
  9. // Redirect to a safe page, e.g., home or login
  10. return next({ path: '/', replace: true });
  11. }
  12. return next();
  13. }