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

Sunday, January 16, 2022

[FIXED] CakePHP login from different views

 January 16, 2022     cakephp, login, php, views     No comments   

Issue

I'm using CakePHP 3.6 and I want to allow the user to login from different views. I have 2 forms that are the same, but one is on every page and the another one is only in 1 page.

Let me show you that:

You can see this mini form is on every page

You can see this mini form is on every page

You can see that this page has the two forms that are the same.

Here's my initialize function of AppControler

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler', [
        'enableBeforeRedirect' => false,
    ]);
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authorize'=> 'Controller',
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
         // If unauthorized, return them to page they were just on
        'unauthorizedRedirect' => $this->referer()
    ]);

}

Here's my login function of UsersController:

public function login()
{

    if($this->request->is('post')) {
        $user = $this->Auth->identify();
        if($user) {
            $this->Auth->setUser($user);
            $this->Flash->success('You logged succesfully!');
            return $this->redirect($this->referer());
        }

        // Invalid login
        $this->Flash->error('Incorrect login');
    }
}

Here's the form (remember they are the same):

<?php if(!$loggedIn): ?>
    <?= $this->Form->create(null, ['class' => 'form-signin']) ?>

        <h2 class="form-signin-heading">Login</h2>
        <?= $this->Form->input('email', ['required' => true, 'class' => 'form-control', 'placeholder' => 'Email', 'label' => false]) ?>
        <?= $this->Form->input('password', ['type' => 'password', 'required' => true, 'class' => 'form-control', 'placeholder' => 'ContraseƱa', 'label' => false]) ?>
        <?= $this->Form->input('remember', ['type' => 'checkbox', 'value' => 'remember-me']) ?>
        <?= $this->Form->submit('Entrar', ['class' => 'btn btn-lg btn-primary btn-block' ]); ?>

    <?= $this->Form->end() ?>
<?php endif; ?>

The problem seems pretty obvious to me but I don't know how to solve it. The form that's in all the views it's not working, but only in the Users view. It seems like the login action it's only working if I'm in the Users view...

Thanks!


Solution

simply change to url in the form that's in every view so that the POST data is sent to users/login

echo $this->Form->create(null, [
    'class' => 'form-signin', 
    'url' => ['controller' => 'Users', 'action' => 'login']
]); 


Answered By - arilia
  • 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