Controller side

Requirements

There's a simple function auth within the PHP trait InjectAuthTrait. This trait requires an instance of JWTTokenManagerInterface to be accessible within the class scope.

class OrderController extends AbstractController
{
    use InjectAuthTrait;

    public function __construct(
        protected JWTTokenManagerInterface $JWTTokenManager,
    ) {}

Usage

Then you only have to wrap the array passed to the view through the render function with the function auth.

return $this->render('restaurant/index.html.twig', $this->auth([
    'restaurant' => $order->getRestaurant(),
]));

View side.

The following script is loaded when the auth function is called on a view. It provides a basic retry mechanism in case the token expires.

The script inject inside the window context an object that contain:

https://github.com/coopcycle/coopcycle-web/blob/e785a97e0d0fe3081eaaf4642175d24cf4ea8117/js/app/user/user-data.js

HttpClient usage

Here is a basic usage of httpClient, which has a method for each HTTP verb.