Skip to content

Instantly share code, notes, and snippets.

@axshia
Last active November 23, 2015 11:15
Show Gist options
  • Save axshia/6a0d28406f476d5697cd to your computer and use it in GitHub Desktop.
Save axshia/6a0d28406f476d5697cd to your computer and use it in GitHub Desktop.
CakePHP CrudPlugin JWT設定例
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'userModel' => 'Users'
],
'ADmad/JwtAuth.Jwt' => [
'storage' => 'Memory',
'parameter' => '_token',
'userModel' => 'Users',
// 'scope' => ['Users.active' => 1],
'fields' => [
'id' => 'id'
]
]
],
'unauthorizedRedirect' => false,
'checkAuthIn' => 'Controller.initialize'
]);
}
public function token()
{
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid username or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = JWT::encode(
[
'id' => $user['id'],
'sub' => $user['id'],
'exp' => time() + 604800,
'iat' => time()
],
Security::salt()
)
],
'_serialize' => ['success', 'data']
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment