简体   繁体   中英

CakePHP login redirect bug

I have the following code in my login method to redirect the user to their previous page and show them a message saying welcome back. I have disabled the autoRedirect to give me control so I can show a flash message and perform other actions on login.

    if(!(empty($this->data)) && $this->Auth->user())
    {
        $this->Session->setFlash('Welcome back! <strong>' . $this->User->field('firstname') . '</strong>', 'flash');

        $this->redirect($this->Auth->redirect($this->referer()));
    }

This works fine when using login forms that are in the header, but when logging in from the actual stand-alone login page it redirects the user BACK to the login page and shows an error saying their username and passwords were incorrect and also the welcome back message. The users IS logged in though and can access the relevant pages BUT I don't want them redirecting back to the login page. How do I fix this?

You can check to see if the referrer is the login page, and if so, redirect them to the home page.

Edited to add a code example:

if($this->referer()=='/users/login'){
    $this->redirect('/');
}else{
    $this->redirect($this->Auth->redirect($this->referer()));
}
if(!(empty($this->data)) && $this->Auth->user())
        {
            $this->Session->setFlash('Welcome back! <strong>' . $this->User->field('firstname') . '</strong>', 'flash');

            //$this->redirect($this->Auth->redirect($this->referer()));

            if($this->referer(array('controller' => 'users', 'action' => 'login')))
            {
                $this->redirect(array('controller' => 'home', 'action' => 'index'));
            }
            else
            {
                $this->redirect($this->Auth->redirect($this->referer()));
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM