User Bundle - After login redirect to URL not working

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

User Bundle - After login redirect to URL not working

Postby Roger1980 » Mon Mar 18, 2013 8:44 pm

Hi all

I have a seriuos problem and do not know how to solve. I do a login on the URL .../login . After the user entered the username and password the user should be redirected to .../profile/username whereas username is of course different from user to user.
The problem is that the redirection is not working the URL is always just .../profile/ which is of course wrong. My code looks like the following:

Code: Select all
<form action="{{ path("fos_user_security_check") }}" method="post">
<!--<form action="" method="post">-->
    <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

    <label for="username">{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" required="required" />

    <label for="password">{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}</label>
    <input type="password" id="password" name="_password" required="required" />

    <input type="checkbox" id="remember_me" name="_remember_me" value="on" />
    <label for="remember_me">{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}</label>

    <input type="hidden" name="_target_path" value="/profile/{{ app.user }}"/>
   
    <input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}" />
</form>


and the line where I do the redirection is the following:

Code: Select all
    <input type="hidden" name="_target_path" value="/profile/{{ app.user }}"/>


What is wrong there? Thank you very much for your help.

Cheers Roger
Roger1980
Junior Member
 
Posts: 27
Joined: Thu Dec 06, 2012 2:23 pm

Re: User Bundle - After login redirect to URL not working

Postby DonPablo » Mon Mar 18, 2013 10:58 pm

Hello,
The problems comes from {{ app.user }}.

When your user is doing a login is not logged at all, so {{ app.user }} doens't contains the user name.
DonPablo
Junior Member
 
Posts: 3
Joined: Mon Mar 18, 2013 10:41 pm

Re: User Bundle - After login redirect to URL not working

Postby DonPablo » Mon Mar 18, 2013 11:02 pm

Hello,
The problems comes from {{ app.user }}.

When your user is doing a login is not logged at all, so {{ app.user }} doens't contains the user name.
DonPablo
Junior Member
 
Posts: 3
Joined: Mon Mar 18, 2013 10:41 pm

Re: User Bundle - After login redirect to URL not working

Postby Roger1980 » Tue Mar 19, 2013 8:38 am

Hi all

Does nobody has a clue how to solve it or is anything missing or not clear? It would be very nice if somebody could help me.

Thanks and cheers

Roger
Roger1980
Junior Member
 
Posts: 27
Joined: Thu Dec 06, 2012 2:23 pm

Re: User Bundle - After login redirect to URL not working

Postby bjo3rn » Tue Mar 19, 2013 10:44 am

See @DonPablo's response. He's correct. To achieve this you'll need a listener for the 'SecurityInteractiveLogin' Event to handle the redirection as app.user is of course not populated at that time.
User avatar
bjo3rn
Faithful Member
 
Posts: 1244
Joined: Fri Jun 17, 2011 10:03 am
Location: Germany

Re: User Bundle - After login redirect to URL not working

Postby Roger1980 » Tue Mar 19, 2013 11:03 am

Ok, thank you very much, I will see what I can find out about the 'SecurityInteractiveLogin' Event and ask you guys in case I need more help.

Thanks again.

Kind regards

Roger
Roger1980
Junior Member
 
Posts: 27
Joined: Thu Dec 06, 2012 2:23 pm

Re: User Bundle - After login redirect to URL not working

Postby Roger1980 » Wed Mar 20, 2013 5:24 pm

Hi all

I tried to build up an SecurityInteractiveLogin Event but now I got a really serious error message which is:

Code: Select all
Catchable Fatal Error: Argument 1 passed to JPP\SecurityBundle\Component\Authentication\Handler\LoginSuccessHandler::__construct() must be an instance of Symfony\Component\Routing\Router, instance of appDevDebugProjectContainer given, called in C:\xampp\htdocs\JPP\app\cache\dev\appDevDebugProjectContainer.php on line 1759 and defined in C:\xampp\htdocs\JPP\src\JPP\SecurityBundle\Component\Authentication\Handler\LoginSuccessHandler.php line 18


My files are looking like the following:

Code: Select all
//JPP/app/config/security.yml
firewalls:
        main:
            pattern:    ^/
            form_login:
                provider: fos_userbundle             
                login_path: /login
                use_forward: false
                check_path: /login_check
                success_handler: jpp_security.component.authentication.handler.login_success_handler
            logout:
                path:   /logout
                target: /
            anonymous: ~

...

parameters:
        jpp_security.component.authentication.handler.login_success_handler.class: JPP\SecurityBundle\Component\Authentication\Handler\LoginSuccessHandler
 
services:
    jpp_security.component.authentication.handler.login_success_handler:
        class: %jpp_security.component.authentication.handler.login_success_handler.class%
        public: false
        arguments:  ['@service_container', '@router', '@security.context']
        tags:
            - { name: 'monolog_logger', channel: 'security' }


Code: Select all
<?php
//JPP/src/JPP/SecurityBundle/Component/Authentication/Handler/LoginSuccessHandler.php

    namespace JPP\SecurityBundle\Component\Authentication\Handler;
   
    use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\Security\Core\SecurityContext;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\Routing\Router;

    class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
    {

        protected $router;
        protected $security;

        public function __construct(Router $router, SecurityContext $security)
        {
            $this->router = $router;
            $this->security = $security;
        }

        public function onAuthenticationSuccess(Request $request, TokenInterface $token)
        {

            if ($this->security->isGranted('ROLE_SUPER_ADMIN'))
            {
                $response = new RedirectResponse($this->router->generate('home'));           
            }
            elseif ($this->security->isGranted('ROLE_ADMIN'))
            {
                $response = new RedirectResponse($this->router->generate('home'));
            }
            elseif ($this->security->isGranted('ROLE_USER'))
            {
                // redirect the user to where they were before the login process begun.
                $referer_url = $request->headers->get('home');

                $response = new RedirectResponse($referer_url);
            }

            return $response;
        }

    }

?>


I really do not know how to solve that. Thank you very much for your help!

Cheers Roger
Roger1980
Junior Member
 
Posts: 27
Joined: Thu Dec 06, 2012 2:23 pm

[SOLVED] User Bundle - After login redirect to URL not worki

Postby Roger1980 » Thu Mar 21, 2013 10:21 am

I was able to solve it, I had one argument to much. But now I got another error which is: Cannot redirect to an empty URL.

Code: Select all
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
        {

            if ($this->security->isGranted('ROLE_SUPER_ADMIN'))
            {
                $response = new RedirectResponse($this->router->generate('home'));           
            }
            elseif ($this->security->isGranted('ROLE_ADMIN'))
            {
                $response = new RedirectResponse($this->router->generate('home'));
            }
            elseif ($this->security->isGranted('ROLE_USER'))
            {
                // redirect the user to where they were before the login process begun.
                $referer_url = $request->headers->get('./route/rogerboehlen');
var_dump($referer_url);
                $response = new RedirectResponse($referer_url);
            }

            return $response;
        }


The code is taking the correct path, namely the elseif ($this->security->isGranted('ROLE_USER')) path, but then the variable $referer_url is NULL and I don't know why, please could you help me! Thank you very much.

Cheers Roger
Roger1980
Junior Member
 
Posts: 27
Joined: Thu Dec 06, 2012 2:23 pm


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: Alexa [Bot] and 3 guests