Last active
November 23, 2015 11:15
-
-
Save axshia/6a0d28406f476d5697cd to your computer and use it in GitHub Desktop.
CakePHP CrudPlugin JWT設定例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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