Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2- Login Form appears again after logged in

Tags:

php

symfony

Hi stackoverflow friends,

I have an instance that ,In symfony2 I created a security bundle (not used FOS USER BUNDLE) in which when I logged in, I try to access the login page again the login form is shown.Is there's no redirection to the default page, even if I'm actually logged in. How to prevent this login form ,after logged in.

Below is my security.yml

UPDATED

firewalls:
        main:
            pattern: ^/
            anonymous: ~
            form_login:
                login_path: /login
                check_path: /login_check
            logout:
                path: /logout
                target: /login    

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/centerreg, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/customredirect, roles: ROLE_USER }
        - { path: ^/admin/, roles: ROLE_ADMIN }
        - { path: ^/center/, roles: ROLE_CENTER }
        - { path: ^/client/, roles: ROLE_CLIENTADMIN }
        - { path: ^/examcenter/, roles: ROLE_EXAMCENTER }
        - { path: ^/tutor/, roles: ROLE_TUTOR }
        - { path: ^/evaluator/, roles: ROLE_EVALUATOR }
        - { path: ^/student/, roles: ROLE_STUDENT }
        - { path: ^/user/, roles: ROLE_USER }

Any help would be appreciable.

like image 493
Asish AP Avatar asked Mar 22 '12 14:03

Asish AP


1 Answers

Symfony won't automatically do this because login page is always accessible as indicated in security.yml:

access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

If you don't want authenticated users to get to the login page, you need to manually redirect them away from login page. To do that, go to your Controller for login action and at the beginning of loginAction() function add this:

public function loginAction()
{
    if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY'))
    {
        // redirect authenticated users to homepage
        return $this->redirect($this->generateUrl('_homepage'));
    }

    //other code goes here...
}

This will redirect authenticated users to your homepage. Of course, replace '_homepage' to route name of a page you wish to redirect users to.

like image 69
Kosta Avatar answered Sep 22 '22 23:09

Kosta