import {CanActivateFn, Router} from '@angular/router';
import {inject} from '@angular/core';
import {AuthService} from '../auth.service';
import {Role} from '../models/role';

export const writerGuard: CanActivateFn = (route, state) => {
  const router = inject(Router);
  const authService = inject(AuthService);
  const authorStr = sessionStorage.getItem("author");

  if (authorStr) {
    if ((authService.isAuthenticated() && JSON.parse(authorStr).role !== Role.WRITER) || !authService.isAuthenticated()) {
      router.navigate(['/']);
    }
    return true;
  }
  return false;
};
