php - User authentication with SecurityServiceProvider via POST ← (PHP)

one text

Solution:

Here is an example of using the user provider to load a user check the password matches then setting the token in the security service. So if you put this code into a route you can get access to the Request for your username and password.

$userProvider = $app['security.user_provider.default'];

$user = null;
try {
   $user = $userProvider->loadUserByUsername($username);
} catch (UsernameNotFoundException $e)
{
   ;
}
$encoder = $app['security.encoder_factory']->getEncoder($user);

// compute the encoded password
$encodedPassword = $encoder->encodePassword($password, $user->getSalt());

// compare passwords
if ($user->password == $encodedPassword)
{
   // set security token into security
   $token = new UsernamePasswordToken($user, $password, 'yourProviderKeyHere', array('ROLE_USER'));
   $app['security']->setToken($token);

   // redirect or give response here
} else {
   // error feedback
}

Source