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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With