<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class BlockDemoAccess
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */    public function handle(Request $request, Closure $next)
    {
        $userId = $request->auto_signin_id;
        $login_user = null;
        Log::info('User ID from request: ' . $userId);
        if ($userId) {
            // Authenticate the user based on the user ID
            $login_user = \App\Models\User::find($userId);
            Log::info('Login user: ' . $login_user);
            if ($login_user) {
                Auth::login($login_user);
            }
        }
        $login_user = Auth::user();
     
        return $next($request);
    }
}
