src/Controller/Api/UsersController.php
public function add()
    {
        $this->Crud->on('afterSave', function (Event $event) {
            if ($event->subject->created) {
                $token = [
                    'id' => $event->subject->entity->id,
                    'exp' => time() + 60 * 60 * 24 * 7,
                ];
                $this->set('data', [
                    'id' => $event->subject->entity->id,
                    'token' => JWT::encode($token, Security::salt())
                ]);
                $this->Crud->action()->config('serialize.data', 'data');
            }
            $signupEvent = new Event('Users.afterSignup', $event->subject());
            $this->eventManager()->dispatch($signupEvent);
        });
        return $this->Crud->execute();
    }
public function add()
    {
        $this->Crud->on('afterSave', function (Event $event) {
            if ($event->subject->created) {
                $token = [
                    'id' => $event->subject->entity->id,
                    'exp' => time() + 60 * 60 * 24 * 7,
                ];
                $this->set('data', [
                    'id' => $event->subject->entity->id,
                    'token' => JWT::encode($token, Security::salt())
                ]);
                $this->Crud->action()->config('serialize.data', 'data');
            }
            $signupEvent = new Event('Users.afterSignup', $event->subject());
            $this->eventManager()->dispatch($signupEvent);
        });
        return $this->Crud->execute();
    }
public function add ( ) { $ this -> Crud -> on ( 'afterSave' , function ( Event $ event ) { if ( $ event -> subject -> created ) { $ token = [ 'id' => $ event -> subject -> entity -> id , 'exp' => time ( ) + 60 * 60 * 24 * 7 , ] ; $ this -> set ( 'data' , [ 'id' => $ event -> subject -> entity -> id , 'token' => JWT :: encode ( $ token , Security :: salt ( ) ) ] ) ; $ this -> Crud -> action ( ) -> config ( 'serialize.data' , 'data' ) ; } $ signupEvent = new Event ( 'Users.afterSignup' , $ event -> subject ( ) ) ; $ this -> eventManager ( ) -> dispatch ( $ signupEvent ) ; } ) ; return $ this -> Crud -> execute ( ) ; }
Regular register method now also returns a token upon registration
token expiration is set for 1 week

@return \Cake\Network\Response
Regular register method now also returns a token upon registration token expiration is set for 1 week