Login redirecting in cakePHP 3.4

ε祈祈猫儿з 提交于 2019-12-08 03:23:53

问题


I'm trying to redirect to current page after logged in, using cakephp 3.4 but I'm getting like this

localhost page isn't working, locahost page redirecting you too many times. Try clearing your cookies

for 2 sec after that it's redirecting to home page. Please help me out here. Here my code

In appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ]
    ]);
}

In loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->referer());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}

回答1:


Looks like you got some redirect loop here. You should use AuthComponent::redirectUrl().

public function login()
{
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();

        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error(__('Username or password is incorrect'));
        }
    }
}

See the Redirecting Users After Login in the Documentation.

After logging a user in, you’ll generally want to redirect them back to where they came from. Pass a URL in to set the destination a user should be redirected to after logging in.

If no parameter is passed, the returned URL will use the following rules:

  • Returns the normalized URL from the redirect query string value if it is present and for the same domain the current app is running on. Before 3.4.0, the Auth.redirect session value was used.
  • If there is no query string/session value and there is a config loginRedirect, the loginRedirect value is returned.
  • If there is no redirect value and no loginRedirect, / is returned.



回答2:


Use $this->Auth->redirectUrl() instead of $this->referer().

After logging a user in, you’ll generally want to redirect them back to where they came from. Pass a URL in to set the destination a user should be redirected to after logging in.

  • Returns the normalized URL from the redirect query string value if it is present and for the same domain the current app is running on. Before 3.4.0, the Auth.redirect session value was used.
  • If there is no query string/session value and there is a config loginRedirect, the loginRedirect value is returned.
  • If there is no redirect value and no loginRedirect, / is returned.

Add to your AuthComponent configuration options:

loginRedirect

The URL (defined as a string or array) to the controller action users should be redirected to after logging in. This value will be ignored if the user has an Auth.redirect value in their session.

Your code should be like that:

In appController.php

public function initialize()
{ 
    parent::initialize();
    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize' => ['Controller'],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ],
                'scope' => ['userStatus' => '1']
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'unauthorizedRedirect' => $this->referer(),
        'logoutRedirect'       => [
                'controller' => 'Users',
                'action'     => 'login'
        ],
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display'     
        ]
    ]);
}

In loginController.php

function login{ 
 if ( $this->request->is( 'post' ) ) {
    if ( $this->Auth->login() ) 
    {
       $this->redirect($this->Auth->redirectUrl());
    } 
    else {
      $this->Flash->error(__('Your username or password is incorrect.'));
    }
  }
}

See also Redirecting Users After Login



来源:https://stackoverflow.com/questions/44257598/login-redirecting-in-cakephp-3-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!