<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class IsAppAccessAuthorized
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (isset(getallheaders()['App-Key']) && (string) getallheaders()['App-Key'] === config('constants.app_key')) {
            return $next($request);
        } 
        else {
            return response()->json([
                'status' => false,
                'error' => config('error_code.invalid_app_key')
            ], 422);
        }
    }
}
