logo
Loading...

How to use JWT in the neoan3 PHP framework

Many things have changed since I last addressed implementing JWT & stateless authentication & authorization.

article image

The code to the video

I am trying out a new format of supplementing my videos with the relevant code-pieces and publishing them as an article. Please let me know if this is helpful or annoying. TY

UserModel.php

changes to the default outgoing method

   /*** @param $method* @param $args* @return mixed*/public static function __callStatic($method, $args){if(!method_exists(self::class, $method)){$transform = new Transform('user', self::$db);return self::outgoing($transform->$method(...self::incoming(...$args)));} else {return self::$method(...$args);}}

changes to the default incoming method

   /*** @param array $transactionResult* @return array*/private static function outgoing(array $transactionResult): array{if(isset($transactionResult['password'])){unset($transactionResult['password']);} elseif (!empty($transactionResult)){foreach ($transactionResult as $i => $single){$transactionResult[$i] = self::outgoing($single);}}return $transactionResult;}

the login method

   /*** @throws RouteException*/static function login($credentials){$foundUser = self::$db->easy('user.id user.password',['email'=>$credentials['email']]);if(empty($foundUser) || !password_verify($credentials['password'],$foundUser[0]['password'])){throw new RouteException('Unauthorized', 401);}return self::get($foundUser[0]['id']);}

AuthController.php

The final version of our authorization controller.

NOTE: In the video, I forgot to address how my IDE automatically includes use-commands. Make sure you include those.

<?phpnamespace Neoan3\Component\Auth;use Neoan3\Core\RouteException;use Neoan3\Frame\Demo;use Neoan3\Model\User\UserModel;use Neoan3\Model\User\UserModelWrapper;use Neoan3\Provider\Auth\Authorization;use Neoan3\Provider\Model\InitModel;/*** Class AuthController* @package Neoan3\Component\Auth** Generated by neoan3-cli for neoan3 v3.**/class AuthController extends Demo{/*** GET: api.v1/auth* GET: api.v1/auth/{id}* GET: api.v1/auth?{query-string}* @return array*/#[Authorization('restrict',['admin'])]function getAuth(): array{return $this->authObject->getPayload();}/*** POST: api.v1/auth* @param string $mode* @param array $body* @return array* @throws \Neoan3\Core\RouteException*/#[InitModel(UserModel::class)]function postAuth(string $mode="Login", array $body =[]): array{if($mode === 'Register'){// create user$newUser = new UserModelWrapper($body);try{$newUser->store()->rehydrate();$user = $newUser->toArray();}catch (\Exception $e) {throw new RouteException('Malformed input', 406);}} else {// try login$user = UserModel::login($body);}$authObject = $this->Auth->assign($user['id'], ['all'], ['email'=>$user['email']]);return ['token' => $authObject->getToken()];}}

Author

author image

neoan

Metrics

Readers: {{metrics.unique}}

Total visits: {{metrics.total}}

Keywords

php,lamp,tutorial,

Suggestions