<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class AccessIpMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        \Log::info($request->ip());
        $accessIps = config("ipWhiteList.access");
        if($accessIps != "*") {
            $accessIps = explode("|", $accessIps);
            if (!in_array($request->ip(), $accessIps)) {
                return response()->json([
                  'message' => "You don't have permission to access this website."
                ], 401);
            }
        }

        return $next($request);
    }
}
