<?php
namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class AuthenticateFromSessionData
{
    public function handle(Request $request, Closure $next): Response
    {
        if (session()->has('user_data') && !Auth::check()) {
            $userArray = session('user_data');
            $userArray['name'] = $userArray['nama_lengkap'];

            $userModel = new User();
            $userModel->forceFill($userArray);

            Auth::login($userModel);
        }
        return $next($request);
    }
}