/**
  * @param  string[]  $login
  * @return IIdentity
  * @throws AuthenticationException
  */
 public function authenticate(array $login) : \Nette\Security\IIdentity
 {
     $user = $this->userRepository->getbyEmail($login[0]);
     if (!$user || !$user->verifyPassword($login[1])) {
         throw new AuthenticationException('user.auth.signInFailed');
     }
     if ($user->isBanned()) {
         throw new AuthenticationException('user.auth.isBanned');
     }
     $this->entityManager->flush($user->signedIn());
     return $user;
 }
Exemple #2
0
 public function login()
 {
     // Build login form
     $form = $this->formFactory->create(new LoginForm());
     // On login attempt (if form was POST-ed and data is valid)
     if ($this->request->getMethod() == 'POST' && $form->handleRequest() && $form->isValid()) {
         // Retrieve POST-ed data
         $data = $form->getData();
         // Try to find user by given login
         /** @var $user \App\Entity\User */
         $user = $this->userRepo->findOneByLogin($data['login']);
         // If user was not found or password check failed
         if (!$user || $user->makePassword($data['password']) != $user->getPassword()) {
             // Display error
             $form->get('password')->addError(new FormError('Login or password is incorrect!'));
         } else {
             // Login user and redirect to home page
             Auth::logIn($user->getId());
             return new RedirectResponse('/');
         }
     }
     // Render page with login form
     return $this->twig->render('login.html.twig', ['Page' => 'login', 'form' => $form->createView()]);
 }