I've been searching the internet and have yet to find a solution to the following problem...

We currently have a website developed using Laravel which the user table is a remote Microsoft SQL database. The driver in config/auth.php has been set to "database". All is working fine except for the password reset functionality, which we get the following error:

UnexpectedValueException in PasswordBroker.php line 238: User must implement CanResetPassword interface.

从我对Laravel的有限理解(这是我第一次体验Laravel)来看,Eloquent 的驱动程序支持CanResetPassword功能,但是,Laravel没有在数据库用户提供程序中实现这一点,因此出现了错误.

因此,我的问题是,有没有人配置了"数据库"的驱动程序并实现了重置密码功能?到目前为止,我看到的所有示例都与使用eloquent模型有关,根据我对Laravel的理解,这不是一种 Select ,因为在最初的开发过程中,我们必须将驱动程序从eloquent更改为数据库,才能使远程Microsoft SQL服务器首先工作.恐怕不能将Microsoft SQL数据库移动到本地数据库.

Alternatively, if anyone has implemented another method of a user resetting their password using an email address I would be open to suggestions.

推荐答案

要编写自己的密码重置逻辑,您仍然可以使用现成的默认迁移,或者只是创建自己的migrations.最重要的部分是 token .由于您正在进行自己的密码重置,您需要做出以下几个决定:

  • Will the token expire?
  • 用户可以多次使用同一令牌吗?

You will need 2 pages, 4 different routes and 4 different functions in the same controller. The 'I forgot my password' page and the 'Reset password' page. In the first page, display a form where you take the user email. And post to the following controller.

//to be added on top as use statements 
use DB;
use Auth;
use Hash;
use Carbon;
use App\User;

public function sendPasswordResetToken(Request $request)
{
    $user = User::where ('email', $request->email)-first();
    if ( !$user ) return redirect()->back()->withErrors(['error' => '404']);

    //create a new token to be sent to the user. 
    DB::table('password_resets')->insert([
        'email' => $request->email,
        'token' => str_random(60), //change 60 to any length you want
        'created_at' => Carbon::now()
    ]);

    $tokenData = DB::table('password_resets')
    ->where('email', $request->email)->first();

   $token = $tokenData->token;
   $email = $request->email; // or $email = $tokenData->email;

   /**
    * Send email to the email above with a link to your password reset
    * something like url('password-reset/' . $token)
    * Sending email varies according to your Laravel version. Very easy to implement
    */
}

第二部分,当用户点击链接时

/**
 * Assuming the URL looks like this 
 * http://localhost/password-reset/random-string-here
 * You check if the user and the token exist and display a page
 */

 public function showPasswordResetForm($token)
 {
     $tokenData = DB::table('password_resets')
     ->where('token', $token)->first();

     if ( !$tokenData ) return redirect()->to('home'); //redirect them anywhere you want if the token does not exist.
     return view('passwords.show');
 }

Display a page with a form containing 2 inputs - New password password or whateveer you want - New password confirmation password_confirm or whatever you want The form should post to the same URL mapped to the following controller. Why? because we still need to use the token to find the actual user.

 public function resetPassword(Request $request, $token)
 {
     //some validation
     ...

     $password = $request->password;
     $tokenData = DB::table('password_resets')
     ->where('token', $token)->first();

     $user = User::where('email', $tokenData->email)->first();
     if ( !$user ) return redirect()->to('home'); //or wherever you want

     $user->password = Hash::make($password);
     $user->update(); //or $user->save();

     //do we log the user directly or let them login and try their password for the first time ? if yes 
     Auth::login($user);

    // If the user shouldn't reuse the token later, delete the token 
    DB::table('password_resets')->where('email', $user->email')->delete();

    //redirect where we want according to whether they are logged in or not.
 }

别忘了添加路由

Route::get('password-reset', 'PasswordController@showForm'); //I did not create this controller. it simply displays a view with a form to take the email
Route::post('password-reset', 'PasswordController@sendPasswordResetToken');
Route::get('reset-password/{token}', 'PasswordController@showPasswordResetForm');
Route::post('reset-password/{token}', 'PasswordController@resetPassword');

Note:可能会有拼写错误或语法错误,因为我没有测试它,而是直接从头顶写下来的.如果你看到错误/异常,不要惊慌,阅读错误并搜索谷歌.

Laravel相关问答推荐

如何在 Laravel 中获取特定月份的最后一个日期?

如何在使用Docker容器部署Laravel 8、9、10时处理SIGTERM信号,特别是与调度程序和工作进程相关的问题?

如何让 Laravel 的 Collection 表现得像一个流?

将 ID 传递给资源控制器进行编辑

spatie 包 laravel-tags 在迁移中没有 down() 函数是有原因的吗

API GET 请求得到与数据库记录不同的结果

如何在数据来自Rest API的flutter中创建下拉类别名称列表?

mysql 加入 ON 和 AND 到 laravel eloquent

如何将用户对象绑定到中间件中的请求

try 发布 AJAX 请求时 POST 405(方法不允许)-Laravel 4

在 laravel 中更改时区

在 Laravel Blade 中转义 vue.js 数据绑定语法?

WhereNotExists Laravel Eloquent

判断输入是否来自控制台

Laravel 验证 pdf mime

Composer RuntimeException - 无法加载软件包 mews/purifier

Laravel 不活动时间设置

Laravel 4 简单路由无法使用 mod_rewrite 和 .htaccess

使用迁移更改表 Laravel 5

函数参数前的三个点代表什么?