Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function initialize()
- {
- parent::initialize();
- $this->Auth->allow(['add', 'token']);
- }
- /**
- * Create new user and return id plus JWT token
- */
- public function add()
- {
- $this->Crud->on('afterSave', function(Event $event) {
- if ($event->subject->created) {
- $this->set('data', [
- 'id' => $event->subject->entity->id,
- 'token' => JWT::encode(
- [
- 'sub' => $event->subject->entity->id,
- 'exp' => time() + 604800
- ],
- Security::salt()
- )
- ]);
- $this->Crud->action()->config('serialize.data', 'data');
- }
- });
- return $this->Crud->execute();
- }
- /**
- * Return JWT token if posted user credentials pass FormAuthenticate
- */
- public function token()
- {
- $user = $this->Auth->identify();
- if (!$user) {
- throw new UnauthorizedException('Invalid username or password');
- }
- $this->set([
- 'success' => true,
- 'data' => [
- 'token' => JWT::encode(
- [
- 'sub' => $user['id'],
- 'exp' => time() + 604800
- ],
- Security::salt()
- )
- ],
- '_serialize' => ['success', 'data']
- ]);
- }
- public function isAuthorized($user)
- {
- return parent::isAuthorized($user);
- }
- $user = $this->Users->newEntity();
- if ($this->request->is('post')) {
- $hostb = $this->request->host();
- $user->userName = '321cdwx11jhagdgt';
- $user->email = 'g3tt@yahoo.com';
- $user->emailConfirmed = true;
- $user->password = 'testing1';
- $user->lastIPAddress = 'testing';
- $user->accessFailedCount = 0;
- $user->lastLockoutDate = new Date('2015-06-15');
- $user->lockoutEndDate = new Date('2015-06-15');
- $user->createdDate = new Date('2015-06-15');
- $user->modifiedDate = new Date('2015-06-15');
- $link = 'http://' . $hostb . '/api/users/register';
- $http = new Client();
- $response = $http->post('http://localhost:8677/api/users/register', json_encode($user), ['type' => 'json']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement