예제 #1
0
 public function login(Request $request, Response $response) : Response
 {
     if ($request->isGet()) {
         return $this->container->view->render($response, 'login.html.twig');
     }
     $input = $request->getParsedBody();
     $form = ['valid' => true, 'fields' => ['username' => ['value' => $input['username']], 'password' => ['value' => '']]];
     if (empty($input['username'])) {
         $form['valid'] = false;
         $form['fields']['username']['error'] = 'Enter username!';
     }
     if (empty($input['password'])) {
         $form['valid'] = false;
         $form['fields']['password']['error'] = 'Enter password!';
     }
     if (!$form['valid']) {
         $this->container->flash->addMessage('form', $form);
         return $response->withRedirect('');
     }
     try {
         $user = $this->container->userRepository->getByUsername($input['username']);
     } catch (\Tablak\ModelNotFoundException $e) {
         $form['valid'] = false;
         $form['fields']['username']['error'] = 'Username not found!';
         $this->container->flash->addMessage('form', $form);
         return $response->withRedirect('');
     }
     if (!password_verify($input['password'], $user->password)) {
         $form['valid'] = false;
         $form['fields']['password']['error'] = 'Wrong password!';
         $this->container->flash->addMessage('form', $form);
         return $response->withRedirect('');
     }
     $_SESSION['user_id'] = $user->id;
     return $response->withRedirect($this->container->router->pathFor('search-tabs'));
 }