Skip to content

Instantly share code, notes, and snippets.

@javiernuber
Last active July 22, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javiernuber/be49eb81ced80329704ef9aab040bfb9 to your computer and use it in GitHub Desktop.
Save javiernuber/be49eb81ced80329704ef9aab040bfb9 to your computer and use it in GitHub Desktop.
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