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:
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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.