Laravel 8 Force password change

Viewed 728

I need suggestions how to make this work.

I want to customize Laravel 8 Jetstream authentication. My project works like this. Once a new user is added, a random password is to be generated. When that user first logs in with that system generated password, it will ask the user to change the password.

Flow I have in mind is to add a column password_changed_at field in my users table, and once the user changes the system generated password, it will populate that column.

I have added the following to JetstreamServiceProvider but I'm kind of stuck how to force the logged in user to redirect strictly to my changepassword.blade.php if password_changed_at field is null.

    public function boot()

{
// ...

Fortify::authenticateUsing(function (Request $request) {
    $user = User::where('email', $request->email)->first();

    if ($user &&
        Hash::check($request->password, $user->password)) {
        return $user;
    }
});

}

I don't want to place an auth check for each controller as I think it's just a repeating code.

Any suggestions how to make this work? Thanks!

1 Answers

I recommend you solve it with middleware. You can create CheckFirstLoginMiddleware and add it to your web group.

Related