Welcome Guest, Not a member yet? Register   Sign In
Urgent: Pass data from filters to controllers
#1

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?
Reply
#2

Create a Constant.
Reply
#3

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

(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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB