2017-02-14 39 views
3

時,我一直在尋找互聯網和還沒有找到一個解決下面的問題,以創建Laravel密碼重置法......如何使用數據庫用戶提供

目前,我們有一個網站開發使用用戶表是一個遠程Microsoft SQL數據庫的Laravel。 config/auth.php中的驅動程序已被設置爲「數據庫」。除密碼重置功能,這是我們得到以下錯誤的所有工作正常:

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

從我有限的Laravel的理解(這是我第一次的experiance與Laravel),雄辯的司機先後爲CanResetPassword功能的支持但是,Laravel並未在數據庫用戶提供程序中實施此操作,因此出現此錯誤。

所以我的問題是,有沒有人有一個配置,他們有驅動程序「數據庫」,並實現了重置密碼功能?我所見過的所有例子都與使用Eloquent模型有關,從我對Laravel的理解來看,這並不是一種選擇,因爲在最初的開發過程中,我們必須將驅動程序從Eloquent更改爲數據庫,以使遠程Microsoft SQL服務器在第一名。將Microsoft SQL數據庫移動到本地數據庫不是我擔心的選項。

另外,如果任何人已經實現了用戶使用電子郵件地址重置他們的密碼的另一種方法,我會接受建議。

+0

我在'config/app.php'中看不到'driver'。但是如果您仍然需要它,我可以幫助您進行自定義密碼重置。 – EddyTheDove

+0

我的不好,它是'config/auth.php',而不是'config/app.php',指定了驅動程序設置。我會糾正這個帖子。非常感謝自定義密碼重置幫助。 –

回答

4

要編寫自己的密碼重置邏輯,您仍然可以使用默認遷移,或者直接創建自己的密碼。最重要的部分是令牌。由於您正在自行設置密碼,因此您需要做出幾項決定:

  • 該令牌是否過期?
  • 用戶可以多次使用相同的標記嗎?

您將需要2頁,4個不同的路線和4個不同的功能在同一個控制器。 '我忘記了我的密碼'頁面和'重置密碼'頁面。在第一頁中,顯示一個表單,您可以在其中接收用戶電子郵件。併發布到以下控制器。

//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'); 
} 

顯示含有2個輸入 形式在頁面上用戶點擊 - 新密碼password或whateveer你想 - 新密碼確認password_confirm或什麼您需要 表單應發佈到映射到以下控制器的相同URL。爲什麼?因爲我們仍然需要使用令牌來查找實際用戶。

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', '[email protected]'); //I did not create this controller. it simply displays a view with a form to take the email 
Route::post('password-reset', '[email protected]'); 
Route::get('reset-password/{token}', '[email protected]'); 
Route::post('reset-password/{token}', '[email protected]'); 

注意:可能有錯別字或語法錯誤,因爲我沒有測試這一點,並在這裏直接從我的頭頂寫的。如果你看到一個錯誤/異常,請不要驚慌失措,閱讀錯誤並搜索谷歌。

+0

謝謝,這是非常有用的。我會盡力實施今天的變化,並讓你知道它是如何去的:-) –

+0

不要忘記標記我的答案和/或upvote答案。 – EddyTheDove

+0

我現在已經實現了密碼重置,只需對數據庫中的SQL數據庫進行微小的更改即可進行修改。再次感謝@EddyTheDove –

相關問題