DEV Community

Cover image for How to use JWT in the neoan3 PHP framework
neoan
neoan

Posted on Originally published at blua.blue

How to use JWT in the neoan3 PHP framework

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 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;
    }
Enter fullscreen mode Exit fullscreen mode

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;
    }

Enter fullscreen mode Exit fullscreen mode

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']);

    }

Enter fullscreen mode Exit fullscreen mode

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.

<?php

namespace 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()];
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)