CakePHP 3 Rest Api JWT 插件表单登录不工作
CakePHP 3 Rest Api JWT plugin Form login Not working
我正在使用 ADmad/cakephp-jwt-auth 插件进行 Json 网络令牌身份验证 api。
为了生成令牌,我们发送电子邮件和密码以通过表单登录。但它失败了,我不知道是什么问题。
这是我的设置。在 AppController.php
$this->loadComponent('Auth', [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'authenticate' => [
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'id' => 'id'
]
]
],
'unauthorizedRedirect' => false,
// Config below is available since CakePHP 3.1.
// It makes user info available in controller's beforeFilter() which is not possible in CakePHP 3.0.
'checkAuthIn' => 'Controller.initialize',
]);
UsersController Token 生成方法脚本;
public function token(){
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid email or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}
我发布的json数据是
{
'email' : 'muni@smart.com',
'password': '123'
}
请告诉我哪里错了?
表单必须在authenticate
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'fields' => [
'id' => 'id'
]
]
]
]);
我正在使用 ADmad/cakephp-jwt-auth 插件进行 Json 网络令牌身份验证 api。
为了生成令牌,我们发送电子邮件和密码以通过表单登录。但它失败了,我不知道是什么问题。
这是我的设置。在 AppController.php
$this->loadComponent('Auth', [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'authenticate' => [
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'id' => 'id'
]
]
],
'unauthorizedRedirect' => false,
// Config below is available since CakePHP 3.1.
// It makes user info available in controller's beforeFilter() which is not possible in CakePHP 3.0.
'checkAuthIn' => 'Controller.initialize',
]);
UsersController Token 生成方法脚本;
public function token(){
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid email or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}
我发布的json数据是
{
'email' : 'muni@smart.com',
'password': '123'
}
请告诉我哪里错了?
表单必须在authenticate
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'fields' => [
'id' => 'id'
]
]
]
]);