CodeIgniter Forums
Urgent: Pass data from filters to controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Urgent: Pass data from filters to controllers (/showthread.php?tid=84147)



Urgent: Pass data from filters to controllers - Mojito - 10-15-2022

Hello,
I'm creating a restapi using jwt authentication.
I'm decoding Jwt hashes in my auth filter like that:
PHP Code:
    public function before(RequestInterface $request$arguments null)
    {
        $header $_SERVER['HTTP_AUTHORIZATION'];
        $token null;

        $response Services::response();
        $response->setJSON(['success' => false]);
        $response->setStatusCode(401);

        if (empty($header)) {
            return $response;
        }

        preg_match('/Bearer\s(\S+)/'$header$matches);
        $token $matches[1];

        if (is_null($token) || empty($token)) {
            return $response;
        }

        $key getenv('JWT_SECRET');
        try {
            $user_id JWT::decode($token, new Key($key'HS256'));
        } catch (\Exception $ex) {
            return $response;
        }
    

And the goal is to pass $user_id variable from the filter to the controller 

What is the best way to do it?


RE: Urgent: Pass data from filters to controllers - plaztic - 10-16-2022

Create a Constant.


RE: Urgent: Pass data from filters to controllers - kenjis - 10-16-2022

It seems better to set the user_id to Request object, but there is no good way to do so.


RE: Urgent: Pass data from filters to controllers - Mojito - 10-16-2022

(10-16-2022, 02:49 PM)kenjis Wrote: It seems better to set the user_id to Request object, but there is no good way to do so.

yes what i did as i didn't found other solution!