<?php
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;

class ResetPasswordController extends Controller
{
    public function showResetForm(Request $request, $token = null)
    {
        return view('auth.passwords.reset')->with([
            'token' => $token,
            'email' => $request->email,
        ]);
    }
    public function reset(Request $request)
    {
        \Log::info('Metode resetPassword dipanggil');
        $request->validate([
            'email' => 'required|email',
            'password' => 'required|confirmed|min:8',
            'token' => 'required',
        ]);
    
        $response = Password::reset(
            $request->only('email', 'password', 'password_confirmation', 'token'),
            function ($user, $password) {
                $user->forceFill([
                    'password' => Hash::make($password),
                ])->save();
            }
        );
    
        if ($response == Password::PASSWORD_RESET) {
            session()->flash('status', 'Password reset successfully.');
        }

        return $response == Password::PASSWORD_RESET
                    ? redirect()->route('login')
                    : back()->withErrors(['email' => [__($response)]]);
    }
}
