Laravel强制用户在首次尝试登录时更改密码

时间:2018-12-05 16:44:58

标签: laravel

Laravel 5.6(5.7)在首次尝试登录时强制更改密码

在我的项目中,我通过使用所有用户的默认/通用密码从excel导入批量上传了用户

问题是,我必须编写一个脚本来强制所有用户在首次登录时更改密码。

1 个答案:

答案 0 :(得分:0)

解决方案是我必须在用户表中添加“ password_changed_at”行,并在我的家庭控制器索引中添加以下代码(因为每个新用户都被定向到家庭)

 public function index()
    {   
        if ((Auth::user()->password_change_at == null)) {
           return redirect(route('change-password'));
        }
        else{

            return view('home');    
        }

    }

我确实使用了setting-up-change-password-with-laravel的更改密码 但我在下面的代码中添加了更新

 //Change Password
        $user = Auth::user();
        $user->password = Hash::make($request->get('new-password'));
        $user->password_change_at = \Carbon\Carbon::now(); //add new line of code
        $user->save();