<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $allowedOrigins = ['http://localhost', 'ionic://localhost'];

          if (in_array($request->server('HTTP_ORIGIN'), $allowedOrigins)) {
              return $next($request)
                  ->header('Access-Control-Allow-Origin', $request->server('HTTP_ORIGIN'))
                  ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
                  ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With, X-XSRF-TOKEN');
          }

        return $next($request);
    }
}
