Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

cakephp - cakephp4 authentication login not working

I am testing the cakephp 4 login authentication. I copied all the code from the cakephp4 tutorial authentication (see below for link) and copied all code to my project as required and no errors were found.

i can create new logins but i cant actually login still. My cakephp4 project is below (its just a test project using test data for the purpose of trying to get logins to work)

https://southernservices.com.au/crm4/users/login

This is the code where things get stuck

public function login()
{
    $this->request->allowMethod(['get', 'post']);
   $result = $this->Authentication->getResult();
    
 
    // regardless of POST or GET, redirect if user is logged in
    debug($result); //debug of output
    if ($result->isValid()) {
        // redirect to /articles after login success
        $redirect = $this->request->getQuery('redirect', [
            'controller' => 'Articles',
            'action' => 'index',
        ]);
        $this->Flash->success(__('Logged In!'));
        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'));
    }
   
}

in application.php here is the main function for authentication
  public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $authenticationService = new AuthenticationService([
        'unauthenticatedRedirect' => '/users/login',
        'queryParam' => 'redirect',
    ]);

    // Load identifiers, ensure we check email and password fields
    $authenticationService->loadIdentifier('Authentication.Password', [
        'fields' => [
            'username' => 'email',
            'password' => 'password',
        ]
    ]);

    // Load the authenticators, you want session first
    $authenticationService->loadAuthenticator('Authentication.Session');
    // Configure form data check to pick email and password
    $authenticationService->loadAuthenticator('Authentication.Form', [
        'fields' => [
            'username' => 'email',
            'password' => 'password',
        ],
        'loginUrl' => '/users/login',
    ]);

    return $authenticationService;
}

https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html

question from:https://stackoverflow.com/questions/65621356/cakephp4-authentication-login-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

debug output:

Login URL https://southernservices.com.au/crm4/users/login did not match /users/login.'

Change:

'/users/login',

to:

'/crm4/users/login'

Or

Router::url(['controller' => 'Users', 'action' => 'login']);

don't forget:

use CakeRoutingRouter;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...