Cakephp 3 Auth Component Does Not Work

Asked

Viewed 77 times

0

I’m having problems with the cake authorization component, I’ve done several tests and it’s the only component that isn’t working, I’ve checked the bank authorization, and I have authorization normally, I’ve reinstalled my system from 0 and do new design but the problem remains, I have already checked the values of my password and username fields and are correct, I have already tested with the encrypted and unencrypted password and nothing either.

I’ve been trying to solve the problem for weeks and I can’t find any solution on the Internet or in the documentation that can help me. The only solution and I think you can have, in my view, is to force a request in the model to do the authorization without using the native component, but I’m not being able to use the cake query itself to do this. Follows my code

Appcontroller

namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Core\Configure;

class AppController extends Controller
{
    public function initialize()
    {
        /*parent::initialize();

        $this->loadComponent('RequestHandler', [
            'enableBeforeRedirect' => false,
        ]);*/
        parent::initialize();

        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ]

                ]
            ],
            'loginRedirect' => [
                'controller' => 'Pagprincipal',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pagprincipal',
                'action' => 'login'
            ]
        ]);

    }

    public function beforeFilter(Event $event) 
    {
        $this->Auth->allow(['index', 'view']);

    }
}

Usercontroller

namespace App\Controller;

use App\Controller\AppController;
class UsersController extends AppController
{
    public function login()
    {
        if($this->request->is('post')){
            $user = $this->Auth->identify();
            if('Users'){
                $this->Auth->setUser('Users');
                return $this->redirect($this->Auth->redirectUrl());
            }else{ 
                $this->Flash->error(__('Username ou senha inválidos, tente novamente!'));
            }   

        }
    }

Login.ctp

<div>
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>

    <div>
        <form>
        <?php
            echo $this->Form->control('username');
            echo $this->Form->control('password');
        ?>
            <p>
                <input type="checkbox" name="remember" id="remember" <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
                <label for="remember-me">Lembrar</label>
            </p>
        </form>
            <button>Entrar</button>
    </div>
</div>

1 answer

0


I found that many things from cake on my pc stopped working, like the Auth component, so I found a solution to be able to authorize my user without using the component. My solution was to search and authorize everything in hand, without using the Auth component.

Appcontroller

public function initialize()
{
    $this->loadComponent('RequestHandler');
    parent::initialize();
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'username',
                    'password' => 'password'
                ]

            ]
        ],
        'loginRedirect' => [
            'controller' => 'Pagprincipal',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Pagprincipal',
            'action' => 'login'
        ],
        //'authorize' => array('Controller')
    ]);
}

Usercontroller

public function login()
{

    $usuario = null;
    if(isset($_POST['username']) && isset($_POST['password'])){

        $username = addslashes($_POST['username']);
        $password = addslashes($_POST['password']);

        $user = TableRegistry::getTableLocator()->get('Users');
        $query = $user
                    ->find()
                    ->where([
                        'username' => $username,
                        'password' => $password
                    ]);
        $results = $query->toArray();
        if(isset($results[0])){
            $name = $results[0]["name"];
            $idUsuario = $results[0]["id"];
            $this->Auth->setUser($results[0]);
            $_SESSION['usuarioLogado'] = $username;
            $_SESSION['nomeUsuario'] = $name;
            $_SESSION['idUsuarioLogado'] = $idUsuario;

            return $this->redirect($this->Auth->redirectUrl());
        }
    }

    if($this->Auth->User){
        return $this->redirect($this->Auth->redirectUrl());
    }

    $this->set('usuario', $usuario);

Login.ctp

<body class="login">
    <?= $this->Flash->render('auth') ?>
    <?= $this->Form->create() ?>

    <?php 

    if(isset($_POST['username']) && isset($_POST['password'])){

        if($usuario == false){
            echo "<div style='padding:10px;text-align:center;'><b>Usuário ou senhas inválidos!</b></div>";
        } 

    }
    ?>

    <div class="logindiv">
        <form>
        <?php
            //echo $this->Form->control('Username:', ['type'=>'username']);
            echo $this->Form->control('username');
            echo $this->Form->control('password');
        ?>
            <p>
                <input type="checkbox" name="remember" id="remember" 
                <?php if(isset($_COOKIE["member_login"])) { ?> checked <?php } ?> />
                <label for="remember-me">Lembrar</label>
            </p>
        </form>
        <button class="loginbuttom" type="Submit">Entrar</button>
    </div>
</body>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.