Last active
July 22, 2016 17:20
-
-
Save javiernuber/be49eb81ced80329704ef9aab040bfb9 to your computer and use it in GitHub Desktop.
AtomicPosts - JWT - Security Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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