Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AtomicPosts - JWT - Security Controller
<?php
// src/AppBundle/Controller/SecurityController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class SecurityController
{
/**
* @Route("/login/{email}/{pass}", name="login")
*/
public function loginAction($email, $pass)
{
$user = $this->getDoctrine()->getRepository('AppBundle:User')
->findOneBy(['email' => $email]);
if(!$user) {
throw $this->createNotFoundException();
}
// Check Password
if(!$this->get('security.password_encoder')->isPasswordValid($user, $pass)) {
throw $this->createAccessDeniedException();
}
// Create JWT token
$token = $this->get('lexik_jwt_authentication.encoder')
->encode(['username' => $user->getUsername()]);
// Return tocken
return new JsonResponse(['token' => $token]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment