Advertisement
Guest User

AuthController

a guest
Apr 25th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Entity\User;
  6. use App\Form\UserType;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  12.  
  13. class AuthController extends Controller
  14. {
  15.     /**
  16.      * @Route("/auth", name="auth")
  17.      */
  18.     public function index()
  19.     {
  20.         return $this->render('auth/index.html.twig', [
  21.             'controller_name' => 'AuthController',
  22.         ]);
  23.     }
  24.  
  25.     /**
  26.      * @Route("/login", name="login")
  27.      */
  28.     public function login(Request $request, AuthenticationUtils $authenticationUtils)
  29.     {
  30.         $username = $authenticationUtils->getLastUsername();
  31.         $error = $authenticationUtils->getLastAuthenticationError();
  32.         return $this->render('auth/login.html.twig', [
  33.             'username' => $username,
  34.             'error' => $error
  35.         ]);
  36.     }
  37.  
  38.     /**
  39.      * @Route("/register", name="register")
  40.      */
  41.     public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
  42.     {
  43.         $user = new User();
  44.         $form = $this->createForm(UserType::class, $user);
  45.         $form->handleRequest($request);
  46.  
  47.         if ($form->isSubmitted() && $form->isValid())
  48.         {
  49.  
  50.             $password = $passwordEncoder->encodePassword($user, $user->getPassword());
  51.             $user->setPassword($password);
  52.             $auth = $this->getDoctrine()->getManager();
  53.             $auth->persist($user);
  54.             $auth->flush();
  55.  
  56.             return $this->redirectToRoute('driver_index');
  57.         }
  58.  
  59.         return $this->render('auth/register.html.twig', [
  60.             'form' => $form->createView(),
  61.         ]);
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement