Skip to content

Commit

Permalink
Update user profile password
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueOb committed Nov 13, 2021
1 parent 28aed40 commit 75ff230
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/Http/Controllers/Profile/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
namespace App\Http\Controllers\Profile;

use App\Http\Controllers\Controller;
use App\Http\Requests\Profile\UpdatePasswordRequest;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class PasswordController extends Controller
{
public function update(Request $request): RedirectResponse
public function update(UpdatePasswordRequest $request): RedirectResponse
{
$validated = $request->validated();

$user = $request->user();
$user->password = Hash::make($validated['password']);
$user->save();

return back()->with('status', 'Password update successfully');
}
}
58 changes: 58 additions & 0 deletions app/Http/Requests/Profile/UpdatePasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Http\Requests\Profile;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Validator;

class UpdatePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'current_password' => ['required', 'string', 'max:255'],
'password' => [
'required',
'string',
Password::min(8)->mixedCase()->numbers()->symbols(),
'confirmed',
'max:255'
],
];
}

/**
* Configure the validator instance.
*
* @param Validator $validator
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(function ($validator) {
if (!Hash::check($this->input('current_password'), $this->user()->password)) {
$validator->errors()->add(
'current_password',
__('The password provided does not match your current password.')
);
}
});
}
}

0 comments on commit 75ff230

Please sign in to comment.