<?php

namespace App\Http\Middleware;

use Closure;

class VerifyApiKey
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->header('Authorization')) {
            $key = $request->bearerToken();
             // Check api with Main
            $data['api_key'] = $key;
            $req = reqGest('POST', 'v1/check_app_key', $data);
             
            if ($req && $req['success']) {
                return $next($request);
            } else {
                return sendError("PERMISSIONS_NOT_GRANTED");
            }
        }
        return sendError("Authorization failed");

    }
}
