import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { ToastrService } from 'ngx-toastr';
import { StorageService } from '../services/storage.service';
@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(
    private router: Router, 
    private toast: ToastrService,
    private storageService: StorageService
  ) {}

  canActivate(): boolean {
    const token = this.storageService.getToken(); 
    if (token) {
      return true; 
    } else {
      this.toast.error('You need to login first');
      this.router.navigate(['auth']); 
      return false; 
    }
  }
}
