<?php

namespace App\User;

class LoginService
{
  public function __construct(UsersRepository $usersRepository)
  {
    $this->usersRepository = $usersRepository;
  }

  public function check() {
    if (isset($_SESSION['login'])) {
      return true;
    } else {
      header("Location: login");
      die();
    }
  }

  public function attempt($username, $password) {
    $user = $this->usersRepository->findByUsername($username);
    if (empty($user)) {
      return false;
    }
    if (password_verify($password, $user->password)) {
      $_SESSION['login'] = $user->username;
      $_SESSION['rechte'] = $user->rechte;

      session_regenerate_id(true);
      //var_dump($_SESSION); die();
      return true;
    } else {
      return false;
    }
  }

  public function logout()
  {
    unset($_SESSION['login']);
    unset($_SESSION['rechte']);
    session_regenerate_id(true);
  }
}
 ?>
