Ich habe das Kennwort route, view und method in [email protected]
und [email protected]
erstellt.
Im Moment, wenn ich das new_password
-Feld ausfülle, wird es gehasht und korrekt an die Datenbank übermittelt, dann kann ich mich mit dem neuen Passwort anmelden.
Ich muss jedoch in der Lage sein, new_password
und new_password_confirm
zu überprüfen, um sicherzustellen, dass sie identisch sind, und das aktuelle Kennwort des Benutzers sowie das aktuelle Kennwort zu überprüfen.
Wie kann ich das machen?
BEARBEITEN: Ich habe der Methode $this->validate
hinzugefügt, aber jetzt bekomme ich weiterhin den Fehler The password confirmation confirmation does not match.
, obwohl sie übereinstimmen, da ich ein einfaches Passwort verwende. Ich denke auch, dass ich das aktuelle Passwort manuell überprüfen muss, da validator
es für mich nicht macht.
public function getProfilePassword(Request $request) {
return view('profile/password', ['user' => Auth::user()]);
}
public function postProfilePassword(Request $request) {
$user = Auth::user();
$this->validate($request, [
'old_password' => 'required',
'password' => 'required|min:4',
'password_confirmation' => 'required|confirmed'
]);
$user->password = Hash::make(Input::get('new_password'));
$user->save();
}
Und das ist die Ansicht
<form action="{{ route('profile/updatepassword') }}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Current Password</label>
<input type="password" name="old_password" class="form-control" id="old_password">
</div>
<div class="form-group">
<label for="name">Password</label>
<input type="password" name="password" class="form-control" id="password">
</div>
<div class="form-group">
<label for="name">New Password</label>
<input type="password" name="password_confirmation" class="form-control" id="password_confirmation">
</div>
<button type="submit" class="btn btn-primary">Change Password</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
Es gibt eine Hash::check()
-Funktion, mit der Sie prüfen können, ob das vom Benutzer eingegebene alte Passwort korrekt ist oder nicht.
usage
if (Hash::check("param1", "param2")) {
//add logic here
}
param1 - user password that has been entered on the form
param2 - old password hash stored in database
wenn das alte Passwort richtig eingegeben wurde, wird es true zurückgegeben, und Sie können Ihre Logik entsprechend hinzufügen
damit new_password
und new_confirm_password
gleich sind, können Sie Ihre Bestätigung in Formularanfragen wie hinzufügen
'new_password' => 'required',
'new_confirm_password' => 'required|same:new_password'
Sie können dies tun, indem Sie eine benutzerdefinierte Überprüfungsregel erstellen (in diesem Beispiel verwende ich current_password
und new_password
als Eingabe).
Setze dies in AppServiceProvider::boot()
:
Validator::extend('current_password', function ($attribute, $value, $parameters, $validator) {
$user = User::find($parameters[0]);
return $user && Hash::check($value, $user->password);
});
Jetzt können Sie in Ihrem Controller Folgendes verwenden:
$user = auth()->user(); // or pass an actual user here
$this->validate($request, [
'current_password' => 'required_with:new_password|current_password,'.$user->id,
]);
Sie können confirmed
hinzufügen, um das alte Kennwort zu bestätigen . Und 'required|confirmed'
ändern Sie in 'required|same:password'
, um password
und password confirmation
zu vergleichen.
'old_password' => 'required|confirmed',
'password' => 'required|min:4',
'password_confirmation' => 'required|same:password'
Viel Glück!
Wenn Sie die Funktionalität einer benutzerdefinierten Regel nur einmal in Ihrer Anwendung benötigen, können Sie anstelle eines Regelobjekts einen Abschluss verwenden. Der Abschluss erhält den Namen des Attributs, den Wert des Attributs und einen $ fail-Rückruf, der aufgerufen werden sollte, wenn die Überprüfung fehlschlägt
$request->validate([
'new_password' => 'required|confirmed|min:4',
'current_password' => ['required', function ($attribute, $value, $fail) use ($user) {
if (!\Hash::check($value, $user->password)) {
return $fail(__('The current password is incorrect.'));
}
}],
]);
Eine komplette Funktion, die alles überprüft. Sie müssen nur old_password
, new_password
und confirm_password
senden.
public function changePassword(Request $request) {
try {
$valid = validator($request->only('old_password', 'new_password', 'confirm_password'), [
'old_password' => 'required|string|min:6',
'new_password' => 'required|string|min:6|different:old_password',
'confirm_password' => 'required_with:new_password|same:new_password|string|min:6',
], [
'confirm_password.required_with' => 'Confirm password is required.'
]);
if ($valid->fails()) {
return response()->json([
'errors' => $valid->errors(),
'message' => 'Faild to update password.',
'status' => false
], 200);
}
// Hash::check("param1", "param2")
// param1 - user password that has been entered on the form
// param2 - old password hash stored in database
if (Hash::check($request->get('old_password'), Auth::user()->password)) {
$user = User::find(Auth::user()->id);
$user->password = (new BcryptHasher)->make($request->get('new_password'));
if ($user->save()) {
return response()->json([
'data' => [],
'message' => 'Your password has been updated',
'status' => true
], 200);
}
} else {
return response()->json([
'errors' => [],
'message' => 'Wrong password entered.',
'status' => false
], 200);
}
} catch (Exception $e) {
return response()->json([
'errors' => $e->getMessage(),
'message' => 'Please try again',
'status' => false
], 200);
}
}
Mit laravel 5.8/6. mache ich Folgendes (ohne viel zusätzlichen Code)
Schritt 1: Validieren
$data = request()->validate([
'firstname' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'username' => ['bail', 'nullable', 'string', 'max:255', 'unique:users'],
'email' => ['bail', 'nullable', 'string', 'email:rfc,strict,dns,spoof,filter', 'max:255', 'unique:users'],
'new_password' => ['nullable', 'string', 'min:8'],
'confirm_new_password' => ['nullable', 'required_with:new_password', 'same:new_password'],
'current_password' => ['required', function ($attribute, $value, $fail) {
if (!\Hash::check($value, Auth::user()->password)) {
return $fail(__('The current password is incorrect.'));
}
}]
]);
Schritt 2: Wenn die Validierung bestanden ist
Zum Beispiel:
if(request(input)){
$data += ['input' => request(input)];
}
Zum Beispiel:
Auth::user()->account->update($data);