src/Middleware/JwtMiddleware.php
/**
     * Invoke middleware.
     *
     * @param ServerRequestInterface $request The request
     * @param RequestHandlerInterface $handler The handler
     *
     * @return ResponseInterface The response
     */
Invoke middleware.
@param ServerRequestInterface $request The request
@param RequestHandlerInterface $handler The handler

@return ResponseInterface The response
Invoke middleware . @param ServerRequestInterface $request The request @param RequestHandlerInterface $handler The handler @return ResponseInterface The response
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $authorization = explode(' ', $request->getHeaderLine('Authorization'));
        $token = $authorization[1] ?? '';
        if (!$token || !$this->jwtAuth->validateToken($token)) {
            return Error::error(
                $this->responseFactory->createResponse(),
                StatusCode::STATUS_UNAUTHORIZED
            );
        }
        $parsedToken = $this->jwtAuth->createParsedToken($token);
        $request = $request->withAttribute('token', $parsedToken);
        $request = $request->withAttribute('uid', $parsedToken->getClaim('uid'));
        return $handler->handle($request);
    }
public function process ( ServerRequestInterface $ request , RequestHandlerInterface $ handler ) : ResponseInterface { $ authorization = explode ( ' ' , $ request -> getHeaderLine ( 'Authorization' ) ) ; $ token = $ authorization [ 1 ] ?? '' ; if ( ! $ token || ! $ this -> jwtAuth -> validateToken ( $ token ) ) { return Error :: error ( $ this -> responseFactory -> createResponse ( ) , StatusCode :: STATUS_UNAUTHORIZED ) ; } $ parsedToken = $ this -> jwtAuth -> createParsedToken ( $ token ) ; $ request = $ request -> withAttribute ( 'token' , $ parsedToken ) ; $ request = $ request -> withAttribute ( 'uid' , $ parsedToken -> getClaim ( 'uid' ) ) ; return $ handler -> handle ( $ request ) ; }
Invoke middleware.
Invoke middleware .
// Append valid token // Append the user id as request attribute