Ejemplo n.º 1
0
 public function login()
 {
     $token = $this->helper->createToken();
     $loginStudentForm = new LoginStudentForm();
     $errors = new ErrorList();
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         $loginStudentForm->fillDataFromArray($_POST);
         $errors = $this->validations->validLoginStudentForm($loginStudentForm);
         if (!$errors->hasErrors()) {
             if ($this->helper->validToken($token)) {
                 $student = $this->studentGateway->getStudentByСolumn('email', $loginStudentForm->getEmail());
                 if ($student and $this->loginHelper->isPasswordValid($student, $loginStudentForm->getPassword())) {
                     $this->loginHelper->createCookies($student);
                     $this->redirect($this->getQuery('go'));
                     exit;
                 } else {
                     $errors->setError('login', "Incorrect username or password");
                 }
             } else {
                 throw new \Exception("Invalid token");
             }
         }
     }
     $this->render('templates/login.phtml', compact('loginStudentForm', 'errors', 'token'));
 }
 public function validLoginStudentForm(LoginStudentForm $loginStudentForm)
 {
     $errors = new ErrorList();
     if ($loginStudentForm->getEmail() === '') {
         $errors->setError('login', 'Email field is empty');
     }
     if ($loginStudentForm->getPassword() === '') {
         $errors->setError('login', 'Password field is empty');
     }
     return $errors;
 }