PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

[FIXED] Cakephp 4.x CMS tutorial authentication redirect problem

 February 03, 2022     cakephp, cakephp-4.x, php     No comments   

Issue

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


Solution

I had this issue as well and found a work around. In my UsersController login method, before return $this->redirect($redirect);, I added this code to strip the extra app name:

if (!is_array($redirect)) {
   if (strncmp($redirect, '/myapp', 6) === 0) {
     $redirect = substr($redirect, 6);
   }
  }

Again, this is a workaround.



Answered By - dividedbyzero
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing