S
S
Sergey Nizhny Novgorod2018-09-05 05:47:48
symfony
Sergey Nizhny Novgorod, 2018-09-05 05:47:48

How to set a session token in Symfony when authenticating?

Hi
I'm doing ajax login in symfony, I encountered the fact that after registration the user session does not appear, therefore any requests of the type:
$request->getSession(); - return null
$request->getUser() - return null
__
What I did:

security.yaml

    firewalls:
        main:
            anonymous: ~
            pattern:    ^/
            http_basic: ~
            provider: our_db_provider

            form_login:
                login_path: index #редиректит сюда, если нет логина
                check_path: login #отрабатывает функцию логина
                success_handler: authentication_handler
                failure_handler: authentication_handler

            logout:
                path: logout
                target: /

Custom class authenticaton_handler
class AuthenticationHandler implements AuthenticationSuccessHandlerInterface,
    AuthenticationFailureHandlerInterface
{
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {
        if ($request->isXmlHttpRequest()) {

            $response = new JsonResponse([
                'reload' => 'reload',
            ]);
            return $response;
        } else {
            $redirect = new RedirectResponse("index");
            return $redirect;
        }
    }

What am I doing here. If the form accepts the correct data, then the standard login mechanism works for me, which transfers control to a custom login that returns a JSON response, and the front uses it to update everything.
The problem is that the login mechanism switches to my handler before it registers all sessions. In theory, I can somehow custom-register a session through TokenInterface $token in my custom class, but I don’t understand how to do it.
Google did not give an answer, there, unfortunately, there is a mess of answers of various lousy answers to all versions of symfony.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fishernet, 2018-09-29
@Fishernet

Symfony 4
Custom can be done like this (if I understand you correctly):

use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class SecurityService
{
    private $authenticationManager;
    private $tokenStorage;

    public function __construct(
        AuthenticationManagerInterface $authenticationManager,
        TokenStorageInterface $tokenStorage,
    )
    {
        $this->authenticationManager = $authenticationManager;
        $this->tokenStorage = $tokenStorage;
    }

    
    public function test()
    {
       
        $user = ...;  // entity User
        
        $unauthenticatedToken = new UsernamePasswordToken(
            $user,
            $user->getPlainPassword(),
            'main'
        );

        $authenticatedToken = $this
            ->authenticationManager
            ->authenticate($unauthenticatedToken);

        $this->tokenStorage->setToken($authenticatedToken);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question