Skip to content

Instantly share code, notes, and snippets.

@imrvelj
Created January 19, 2017 13:10
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 imrvelj/75740af51707e2e52a06f5dbf728965d to your computer and use it in GitHub Desktop.
Save imrvelj/75740af51707e2e52a06f5dbf728965d to your computer and use it in GitHub Desktop.
$username = $request->request->get('username');
$password = $request->request->get('password');
if(is_null($username) || is_null($password)) {
return new Response(
'Please verify all your inputs.',
Response::HTTP_UNAUTHORIZED,
array('Content-type' => 'application/json')
);
}
$user_manager = $this->get('fos_user.user_manager');
$factory = $this->get('security.encoder_factory');
$user = $user_manager->findUserByUsername($username);
$encoder = $factory->getEncoder($user);
$salt = $user->getSalt();
if($encoder->isPasswordValid($user->getPassword(), $password, $salt)) {
$response = new Response(
'Welcome '. $user->getUsername(),
Response::HTTP_OK,
array('Content-type' => 'application/json')
);
} else {
$response = new Response(
'Username or Password not valid.',
Response::HTTP_UNAUTHORIZED,
array('Content-type' => 'application/json')
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment