- Which part are you unsure of how to proceed?
- When a user logs in, their passwords will be sent to you in plaintext, that's the time you can compare against the new rules
Force user to update password
Hello,
I have a situation where I want to increase the password requirements on an existing project.
I have created a Custom Rule and updated my Register and Reset Password Controllers - everything fine.
However, I now want to look at updating users where their current password no longer meets the new security requirements.
I thought something along the lines of - User logs in, current password is checked against the new rule, if it doesn't meet the new requirements, they are forced to update it and can't navigate anywhere else in the site.
Maybe that is an over-engineer - I'm not sure how to proceed. Any thoughts / advice / sample code appreciated.
Thanks
- Set a
weak_passwordsession key in yourattemptLoginto flag out this user - Have a middleware that checks for
weak_passwordand redirect user - On
/myaccount/change-password, after the you are satisfied with the new password, you needsession()->forget('weak_password');to unflag this user
protected function attemptLogin(Request $request)
{
$successfulAuth = $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
// check if the user was able to authenticate
if ($successfulAuth == true) {
// check if password meetings new requirements
if (preg_match("/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@()$%^&*=_{}[\]:;\"'|\<>,.\/~`±§+-]).{12,30}$/", $request->password)) {
// user can proceed
return $successfulAuth;
} else {
// user has auth'ed but the password isn't strong enough
// user needs to be forced to /myaccount/change-password
session(['weak_password' => true]);
}
}
}
Middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\App;
class weak_password
{
protected $except = [
'/myaccount/change-password', // allow this URL to load even if "weak_password" exists
// add more for your change-password CRUD URLs
];
protected function inExceptArray($request)
{
foreach ($this->except as $except) {
if ($except !== '/') {
$except = trim($except, '/');
}
if ($request->fullUrlIs($except) || $request->is($except)) {
return true;
}
}
return false;
}
public function handle($request, Closure $next)
{
if (!$this->inExceptArray($request) || session("weak_password")) {
return redirect()->secure("/myaccount/change-password");
}
return $next($request);
}
}
Please or to participate in this conversation.