John Davidson

php - Cakephp 4.x CMS tutorial authentication redirect problem

0 comments
Message:


I just recreated the CMS tutorial to the letter. When not logged in and trying to edit an article I'm redirected to the login page. Success here.
After logging in I get redirected to an error page. Why? The redirect back to articles has gone wrong. See below


Link it is redirecting to: http://localhost/test_cms/test_cms/articles/edit/first-post


Link it is supposed to redirect to http://localhost/test_cms/articles/edit/first-post


I worked every file according to the CMS tutorial and can't seem to figure out what is going wrong here.


Login code below:


public function login()
{
$this->Authorization->skipAuthorization();

$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
// redirect to /articles after login success
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Articles',
'action' => 'index',
]);
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}

Error screenshot: https://ibb.co/PjX8435



My Answer:
Authentication->setConfig('loginRedirect', ['controller' => 'Articles', 'action' => 'index']);_ $this->Authentication->setConfig('logoutRedirect', ['controller' => 'Pages', 'action' => 'display', 'home']);_ $result = $this->Authentication->getResult();_ if ($this->request->is('post') && $result->isValid()) {_ $target = $this->Authentication->getLoginRedirect() ?? '/';_ return $this->redirect($target);_ }_}_Make sure that the loginRedirect configuration is set correctly in your login function. The redirect should be set to the correct controller and action for the articles edit page. If the redirect is still not working, you may need to check your routes configuration and make sure that the correct routes are set up for the articles controller.

Rate this post

5 of 5 based on 3609 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us