簡體   English   中英

laravel 5.1 Password::reset 返回passwords.password

[英]laravel 5.1 Password::reset returns passwords.password

我在控制器中有此功能,但我無法重置密碼,因為我想將字符長度更改為 5 位數字。

public function postReset(Request $request)
{
  $this->validate($request, [
    'token' => 'required',
    'password' => 'required|confirmed|digits:5',
  ]);

  $credentials = $request->only(
    'email', 'password', 'password_confirmation', 'token'
  );

  $response = Password::reset($credentials, function ($user, $password) {
    $this->resetPassword($user, $password);
  });

  dd($response);
  switch ($response) {
    case Password::PASSWORD_RESET:
      return redirect($this->redirectPath());

    default:
      return redirect()->back()
        ->withInput($request->only('email'))
        ->withErrors(['email' => trans($response)]);
  }
}

protected function resetPassword($user, $password)
{
  $user->password = bcrypt($password);
  $user->save();
  Auth::login($user);
}

但它總是說:

哎呀! 您的輸入存在一些問題。

密碼必須至少為六個字符並與確認相符。

當我添加:

dd($response);

它打印:

密碼.密碼

知道如何解決這個問題嗎?

發生這種情況是因為Illuminate\\Auth\\Passwords\\PasswordBroker存在硬編碼驗證。

調用reset方法時,它總是先調用validateReset ,然后調用validateNewPassword

public function validateNewPassword(array $credentials)
{
    list($password, $confirm) = [
        $credentials['password'],
        $credentials['password_confirmation'],
    ];

    if (isset($this->passwordValidator)) {
        return call_user_func(
            $this->passwordValidator, $credentials) && $password === $confirm;
    }

    return $this->validatePasswordWithDefaults($credentials);
}

默認情況下,未設置passwordValidator 因此validatePasswordWithDefaults將要求密碼長度至少為 6 個字符。

您可以使用Password::validator設置passwordValidator ,它接受一個閉包,該閉包必須返回一個布爾值,指示給定的憑據是否有效。 這需要在Password::reset之前完成。

例如,將驗證器更改為要求密碼長度恰好為 5 個字符將特別滿足您的要求。

Password::validator(function($credentials)
{
    return strlen($credentials['password']) === 5;
});

你正在尋找的是在這個類中:

\Illuminate\Auth\Passwords\PasswordBroker

和這個功能

validatePasswordWithDefaults

6在這個函數中被硬編碼,這看起來有點奇怪。 我想可能有更好的做法來改變它。 也許您可以覆蓋控制器中的功能。 也試試。

改變這一行:

'password' => 'required|confirmed|digits:5'

'password' => 'required|confirmed|min:5'

如果您想更改文本並覆蓋 passwords.password,請按照以下步驟操作

首先轉到PasswordBroker.php

vendor\laravel\framework\src\Illuminate\Contracts\Auth\PasswordBroker.php

轉到第 35 行並在此處更改

const INVALID_PASSWORD =  '**any thing you want**';

或者,如果您想更改驗證,請按照以下步驟操作

首先轉到PasswordBroker.php

vendor\laravel\framework\src\Illuminate\Auth\Passwords\PasswordBroker.php

轉到第 176 行並在此處更改

return $password === $confirm && mb_strlen($password) >= **any digit you want**;

然后去ResetsPasswords.php

vendor\laravel\framework\src\Illuminate\Foundation\Auth\ResetsPasswords.php

轉到第 69 行並在此處更改

'password' => 'required|confirmed|min:**degit you chose in **PasswordBroker.php****',

謝謝...它有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM