Skip to content

Instantly share code, notes, and snippets.

@Kim-Vallee
Created July 12, 2018 09:30
Show Gist options
  • Save Kim-Vallee/6fe77a3557181b2dba0b77b87021dbe4 to your computer and use it in GitHub Desktop.
Save Kim-Vallee/6fe77a3557181b2dba0b77b87021dbe4 to your computer and use it in GitHub Desktop.
<div class="page-wrapper">
{% if app.flashes %}
<div class="container">
{% for label, messages in app.flashes %}
{% for message in messages %}
<div class="alert alert-{{ label }} alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
{{ message }}
</div>
{% endfor %}
{% endfor %}
</div>
{% endif %}
<div class="background"></div>
{% block body %}{% endblock %}
</div>
<?php
namespace App\Controller;
use \DateTime;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Form\UtilisateurLoginType;
use App\Form\UtilisateurSignupType;
use App\Entity\Utilisateur;
use App\DTO\UtilisateurSignupData;
use App\Entity\Entreprise;
class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
$session = $this->get('session');
if($session->get('_security.main.target_path')) {
/*$this->addFlash('error', 'Vous devez être authentifié pour continuer');
return $this->redirectToRoute('homepage');*/
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// Build form
// If the username exists
if ($lastUsername) {
$opt = ['_username' => $lastUsername];
} else {
$opt = [];
}
$form = $this->createForm(UtilisateurLoginType::class, $opt);
return $this->render('security/login.html.twig', array(
'form' => $form->createView(),
'error' => $error,
'titre' => 'Login',
));
}
/**
* @Route("/signup", name="signup")
*/
public function signup(Request $request, UserPasswordEncoderInterface $passwordEncoder)
{
// Repository to check emails and usernames
$repository = $this->getDoctrine()->getRepository(Utilisateur::class);
// Build form with data
$utilisateurData = new UtilisateurSignupData($repository);
$form = $this->createForm(UtilisateurSignupType::class, $utilisateurData);
// Handle form request if exists
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Using DTO
$utilisateur = new Utilisateur();
$utilisateur->setEmail($utilisateurData->getEmail());
$utilisateur->setUsername($utilisateurData->getUsername());
$utilisateur->setPassword($utilisateurData->getPassword());
// Encode password
$password = $passwordEncoder->encodePassword($utilisateur, $utilisateur->getPassword());
$utilisateur->setPassword($password);
// Saving !
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($utilisateur);
$entityManager->flush();
$this->addFlash('success', 'Inscrit avec succès ! Vous pouvez désormais vous connecter '.$utilisateur->getUsername());
return $this->redirectToRoute("homepage");
}
return $this->render('security/signup.html.twig', array(
'form' => $form->createView(),
'titre' => 'Inscription',
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment