Redirect to Request page in hwioauthbundle [closed]
-
02-01-2020 - |
Domanda
Suppose I have a page http://example.com/page1
I have placed login with facebook link here.
And also on another page,page2, I have placed login link with facebook.
What I want is that after clicking login with facebook link on page1, it should redirect to page1 and on clicking login link on page2 , it should redirect to Page2.
Right now,it always redirect to 'example.com/'.
I am using Hwioauthbundle in integration with fosuserbundle in Symfony2
Soluzione
1) Create an authentication handler
<?php
namespace Company\Bundle\Handler;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
class SecurityHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$referer = $request->headers->get('referer');
if (empty($referer)) {
return new RedirectResponse($this->router->generate('homepage'));
} else {
return new RedirectResponse($referer);
}
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
// Edit it to meet your requeriments
$request->getSession()->set('login_error', $error);
return new \Symfony\Component\HttpFoundation\RedirectResponse($this->router->generate('login_route'));
}
}
2) Register it as service
# src/Company/Bundle/Resources/config/services.yml
security_handler:
class: Company\Bundle\Handler\SecurityHandler
arguments: [@router]
3) Config HWIO to use this service as handler
# app/config/security.yml
firewalls:
# ....
you_firewall:
oauth:
# ....
resource_owners:
# ....
success_handler: security_handler
failure_handler: security_handler # optional
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow