Symfony 3 bug on how to create a traditional login form

Trying to use the tutorial on these pages

The problem is that I always return to the login form and I can’t finf what I am missing. I am using Synfony version 3. Thank you for your help :)

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
    providers:
        in_memory:
            memory: 
                users:
                    ryan:
                        password: ryanpass
                        roles: 'ROLE_USER'
                    admin:
                        password: admin
                        roles: 'ROLE_USER'
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false


        home:
            pattern: ^/home$
            form_login:
                login_path: login
                check_path: login
                always_use_default_target_path: true

        main:
            anonymous: ~


=========== routing
login:
    path:   /login
    defaults:  { _controller: AppBundle:Security:login }

btw_user:
    resource: "@BTWUserBundle/Resources/config/routing.yml"
    prefix:   /

btw_menus:
    resource: "@BTWMenusBundle/Resources/config/routing.yml"
    prefix:   /

app:
    resource: "@AppBundle/Controller/"
    type:     annotation

btw_home:
    resource: "@BTWHomeBundle/Resources/config/routing.yml"
    prefix:   /


============ controller
namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;


class SecurityController extends Controller {

    public function loginAction(Request $request) {
        //var_dump($request);

        $authenticationUtils = $this->get("security.authentication_utils");
        //var_dump($_POST);
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError ();
        var_dump($error);
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();
        var_dump($lastUsername);
        return $this->render ( 'security/login.html.twig',
                array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        ));
    }
}
+4
source share
1 answer

I also had a problem, this solved my problem:

context: session_key Read more

And creating an additional form_login in the admin_login yaml block.

This is not the best way to fix this problem, and I would do tests to avoid this, but it works!

Using:

admin_login:
  pattern:  ^/admin/login$
  anonymous: ~
  provider: user_provider
  context: session_key
  form_login:
    login_path: admin_login
    check_path: admin_login
    default_target_path: admin_category_index
admin:
  pattern: ^/admin
  provider: user_provider
  context: session_key
  form_login:
    login_path: admin_login
    check_path: admin_login
    default_target_path: admin_category_index
  logout:
    path:   /logout
    target: /
    invalidate_session: false
0
source

Source: https://habr.com/ru/post/1627931/


All Articles