<?php

namespace App\Http\Middleware;

use App\Support\LoginCredentials;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class SanitizeLoginRequest
{
    /**
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->routeIs('login.store') && $request->isMethod('POST')) {
            $request->merge([
                'email' => LoginCredentials::sanitizeEmail($request->input('email')),
                'password' => LoginCredentials::sanitizePassword($request->input('password')),
            ]);
        }

        return $next($request);
    }
}
