2
votes

I've just started working through the Symfony 2 tutorials. I have created a bundle with a user class and have tried to follow the instructions to set up a login process. I think I am nearly there, however I'm currently falling at the last hurdle.

I have set up a bundle: Dan\AuthBundle, which contains my user class and another bundle: Dan\HelloBundle which I want to allow only logged in users to access.

My security.yml file is as follows:

security:
    encoders:
        Dan\AuthBundle\Entity\User: sha512

    providers:
        main:
            entity: { class: Dan\AuthBundle\Entity\User, property: username }
        administrators:
            entity: { class: DanAuthBundle:User }

    firewalls:
        secured_area:
            pattern:    ^/*
            form_login:
                check_path: /login_check
                login_path: /login
                always_use_default_target_path: false
                default_target_path: /hello

    access_control:
        - { path: ^/hello/.* }

The main routing.yml file looks like this:

DanAuthBundle:
    resource: "@DanAuthBundle/Resources/config/routing.yml"
    prefix:   /auth/

DanHelloBundle_homepage:
pattern:  /hello/
defaults: { _controller: DanHelloBundle:Default:index }

login:
    pattern: /login
    defaults: {_controller: DanAuthBundle:Default:login }

login_check:
    pattern: /login_check

I have created several instances of my user class manually.

If I try to access the url /hello, I correctly get redirected to the login page. If I enter incorrect details, I get the correct message(s) delivered in the template, however, when I log in with the correct details, I receive a 324 (empty response) error (at this time, the url displayed in the browser is login_check).

From reading the documentation, I thought I should be redirected to the page I was originally trying to access?

http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

By default, if the submitted credentials are correct, the user will be redirected to the original page that was requested (e.g. /admin/foo). If the user originally went straight to the login page, he'll be redirected to the homepage. This can be highly customized, allowing you to, for example, redirect the user to a specific URL.

Also, if I try to access the page after entering the correct details, I once again get redirected to the login page.

Can anyone see if I've missed anything obvious?

This is from my log file:

[2012-06-18 18:33:47] doctrine.DEBUG: SELECT t0.id AS id1, t0.username AS username2, t0.salt AS salt3, t0.hashed_password AS hashed_password4 FROM User t0 WHERE t0.username = ? (["hello"]) [] [] [2012-06-18 18:33:47] security.INFO: User "hello" has been authenticated successfully [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Component\Security\Http\Firewall::onKernelRequest" stopped propagation of the event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener" was not called for event "kernel.request". [] [] [2012-06-18 18:33:47] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\Firewall\ContextListener::onKernelResponse". [] [] [2012-06-18 18:33:47] security.DEBUG: Write SecurityContext in the session [] []

Any advice appreciated.

Thanks.

3
you can try to add a login listener to redirect your user : metod.si/login-event-listener-in-symfony2Snroki
Here is also my answer with target_path: stackoverflow.com/a/17424321/1866083nvvetal

3 Answers

7
votes
// if you're using Symfony 2.0
$key = '_security.target_path';
// if you're using Symfony 2.1 or greater
// where "main" is the name of your firewall in security.yml
$key = '_security.main.target_path';

// try to redirect to the last page, or fallback to the homepage
if ($this->container->get('session')->has($key)) {
  $url = $this->container->get('session')->get($key);
  $this->container->get('session')->remove($key);
} else {
  $url = $this->container->get('router')->generate('homepage');
}

return new RedirectResponse($url);
4
votes

You need 2 listeners.

  • One to set in session last page

  • Second to redirect after succesfull login

That link will solve your problem: http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/

-3
votes

just use getUser check in your respective action (where you are rendering the login form view) as below:

if($this->getUser()){ return $this->redirect($this->generateUrl('your-redirect-path-alise')); }