问题描述
我正在测试 cakePHP 4 登录身份验证。我从 cakePHP4 教程认证中复制了所有代码(见下面的链接),并根据需要将所有代码复制到我的项目中,没有发现错误。
我可以创建新的登录,但我实际上仍然无法登录。我的 cakePHP4 项目如下(它只是一个使用测试数据的测试项目,目的是尝试让登录工作)
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',],'loginUrl' => '/users/login',]);
return $authenticationService;
}
https://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html
解决方法
调试输出:
登录 URL https://southernservices.com.au/crm4/users/login
没有
匹配 /users/login
。'
变化:
'/users/login',
到:
'/crm4/users/login'
或
Router::url(['controller' => 'Users','action' => 'login']);
不要忘记:
use Cake\Routing\Router;