Validation untuk cek password lama di Laravel

Membuat validation untuk melakukan pengecekan password lama pada fitur ubah password di Laravel.

Buat rule baru dengan perintah berikut, nama file/rule bebas

php artisan make:rule Passlama

File yang baru dibuat ada di folder app/Rules/Passlama.php

Laravel 10

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

public function validate(string $attribute, mixed $value, Closure $fail): void
{
    // pengecekan password hash untuk user yang login
    $isPasswordValid = Hash::check($value, Auth::user()->password);

    // jika password tidak sesuai
    if (!$isPasswordValid) {
        $fail('Password lama salah');
    }
}

Laravel 9

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

// ...

public function passes($attribute, $value)
{
    // pengecekan password hash untuk user yang login
    return Hash::check($value, Auth::user()->password);
}

public function message()
{
    // pesan jika password tidak sesuai
    return 'Password Lama salah';
}

Controller

Di controller yang akan menggunakan rule Passlama

use Illuminate\Support\Facades\Hash;
use App\Models\User;
use App\Rules\Passlama;

class PasswordController extends Controller
{
    public function index()
    {
        return view('password');
    }

    public function proses(Request $request)
    {
        // pesan validation
        $messages = [
            'required' => ':attribute harus diisi',
            'confirmed' => ':attribute harus sama',
        ];

        // validation password lama
        $this->validate($request, [
            'password_lama' => ['required', new Passlama],
            'password' => 'required|confirmed',
            'password_confirmation' => 'required',
        ], $messages);

        // update password user
        $user = User::find(auth()->user()->id);
        $data = [
            'password' => Hash::make($request->password),
        ];
        $user->update($data);

        return back()->with(['success' => 'Password berhasil diubah']);
    }
}

View

Contoh view menggunakan Bootstrap 5

<div class="card mb-4">
    <div class="card-header"><strong>Ubah Password</strong></div>
    <div class="card-body">
        <form action="{{ route('password.proses') }}" method="post">
            @csrf
            <div class="form-group mb-3">
                <label for="password_lama">Password Lama</label>
                <input type="password" class="form-control @error('password_lama') is-invalid @enderror" name="password_lama" id="password_lama" value="{{ old('password_lama') }}">
                @error('password_lama')
                    <div class="invalid-feedback">
                        {{ $message }}
                    </div>
                @enderror
            </div>
            <div class="form-group mb-3">
                <label for="password">Password Baru</label>
                <input type="password" class="form-control @error('password') is-invalid @enderror" name="password" id="password">
                @error('password')
                    <div class="invalid-feedback">
                        {{ $message }}
                    </div>
                @enderror
            </div>
            <div class="form-group mb-3">
                <label for="password_confirmation">Konfirmasi Password Baru</label>
                <input type="password" class="form-control @error('password_confirmation') is-invalid @enderror" name="password_confirmation" id="password_confirmation">
                @error('password_confirmation')
                    <div class="invalid-feedback">
                        {{ $message }}
                    </div>
                @enderror
            </div>
            <div class="mt-4">
                <button type="submit" class="btn btn-outline-success">Ubah</button>
            </div>
        </form>
    </div>
</div>

Route

Route::get('/password', [PasswordController::class, 'index'])->name('password')->middleware('auth');
Route::post('/password/proses', [PasswordController::class, 'proses'])->name('password.proses')->middleware('auth');