Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public function initialize()
  2. {
  3. parent::initialize();
  4. $this->Auth->allow(['add', 'token']);
  5. }
  6.  
  7. /**
  8. * Create new user and return id plus JWT token
  9. */
  10. public function add()
  11. {
  12. $this->Crud->on('afterSave', function(Event $event) {
  13. if ($event->subject->created) {
  14. $this->set('data', [
  15. 'id' => $event->subject->entity->id,
  16. 'token' => JWT::encode(
  17. [
  18. 'sub' => $event->subject->entity->id,
  19. 'exp' => time() + 604800
  20. ],
  21. Security::salt()
  22. )
  23. ]);
  24. $this->Crud->action()->config('serialize.data', 'data');
  25. }
  26. });
  27. return $this->Crud->execute();
  28. }
  29.  
  30. /**
  31. * Return JWT token if posted user credentials pass FormAuthenticate
  32. */
  33. public function token()
  34. {
  35. $user = $this->Auth->identify();
  36. if (!$user) {
  37. throw new UnauthorizedException('Invalid username or password');
  38. }
  39.  
  40. $this->set([
  41. 'success' => true,
  42. 'data' => [
  43. 'token' => JWT::encode(
  44. [
  45. 'sub' => $user['id'],
  46. 'exp' => time() + 604800
  47. ],
  48. Security::salt()
  49. )
  50. ],
  51. '_serialize' => ['success', 'data']
  52. ]);
  53. }
  54.  
  55. public function isAuthorized($user)
  56. {
  57. return parent::isAuthorized($user);
  58. }
  59.  
  60. $user = $this->Users->newEntity();
  61. if ($this->request->is('post')) {
  62. $hostb = $this->request->host();
  63. $user->userName = '321cdwx11jhagdgt';
  64. $user->email = 'g3tt@yahoo.com';
  65. $user->emailConfirmed = true;
  66. $user->password = 'testing1';
  67. $user->lastIPAddress = 'testing';
  68. $user->accessFailedCount = 0;
  69. $user->lastLockoutDate = new Date('2015-06-15');
  70. $user->lockoutEndDate = new Date('2015-06-15');
  71. $user->createdDate = new Date('2015-06-15');
  72. $user->modifiedDate = new Date('2015-06-15');
  73. $link = 'http://' . $hostb . '/api/users/register';
  74. $http = new Client();
  75. $response = $http->post('http://localhost:8677/api/users/register', json_encode($user), ['type' => 'json']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement