Zfc-rbac redirect to referrer after login

144 Views Asked by At

Is there a way for me redirect to the referring url after logging in?

I have this in my config:

'redirect_strategy' => [
    'redirect_when_connected'        => false,
    'redirect_to_route_connected'    => 'home',
    'redirect_to_route_disconnected' => 'login',
    'append_previous_uri'            => true,
    'previous_uri_query_key'         => 'redirectTo'
],

the previous_uri_query_keyonly is appended when a user goes to a page that they do not have permissions for. An example would be someone is browsing and then login, but would like to continue browsing where they left off.

I have tried to use $_SERVER['HTTP_REFERER'] on the login action, but I am running that action to load the page and then once they post their credentials it logs them in. Doing it this way the server now thinks the referrer is the login page and just takes you back there.

There is probably a simple solution and I am just not seeing it currently.

******************Jerry-Rig******************

So this is what I ended up using for the time being. I am not going to put this as the answer since I would like a better solution. If I have time to go back and fix this I will post my solution. But for now here is what I am using.

if (null === $this->params()->fromQuery('redirect') && $this->shouldAddRedirect($request->isPost()) &&  !$request->isPost()) {
    return $this->redirect()->toUrl('sign-up?redirect=' . $_SERVER['HTTP_REFERER']);
}

The function I call shouldAddRedirect looks like this

private $no_redirect = array(
    '/login',
    '/sign-up'
);

protected function shouldAddRedirect($isPost) {
    if (isset($_SERVER['HTTP_REFERER'])){
        $referer_url = parse_url($_SERVER['HTTP_REFERER']);
        if (isset($referer_url['query']) && count($referer_url['query'])) {
            return false;
        }
        if (isset($referer_url['path']) && in_array($referer_url['path'], $this->no_redirect)) {
            return false;
        }
    } else if ($isPost) {
        return false;
    }

    return true;
}
0

There are 0 best solutions below