<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CorsSendAppointment
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $allowedOrigins = [
           "http://localhost",
           "https://localhost",
           "http://doctorantonio.net",
           "https://doctorantonio.net"
        ];
        
        if (in_array($request->server('HTTP_ORIGIN'), $allowedOrigins)) {
           return  $next($request)
                ->header('Access-Control-Allow-Origin', $request->server('HTTP_ORIGIN'))//$request->server('HTTP_ORIGIN'))
                ->header("Access-Control-Allow-Credentials","true")
                ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
                ->header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Token-Auth, Authorization, X-CSRF-Token"); 
        }
       
         return $next($request);
    }
}
