<?php

namespace App\Http\Middleware;

use App\Services\ApiJwt;
use Closure;

/**
 * Decodes Authorization: Bearer &lt;access JWT&gt; and merges accessToken / memId / resourceType
 * into the request so controllers can use $request->input('accessToken') without duplicating it in the body.
 * Runs globally for all routes (apiGateway, preferences, etc.).
 */
class MergeJwtBearer
{
    /**
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $jwtResponse = ApiJwt::mergeBearerIntoRequest($request);
        if ($jwtResponse !== null) {
            return $jwtResponse;
        }

        return $next($request);
    }
}
