Laravel 提供了一种通过覆盖某些函数来更改身份验证(电子邮件、密码)的默认列的方法。
在你的用户模型中添加这个覆盖密码默认列的函数:
App/User.php
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->user_pass;
}
并且,在您的 LoginController 中从 email 更改为 user_email
App/Http/Controllers/Auth/LoginController.php
/**
* Get the login username to be used by the controller.
*
* @return string
*/
public function username()
{
return 'user_email';
}
现在你已经覆盖了 Laravel 的 Auth 逻辑使用的默认列。但你还没有完成。
LoginController 具有验证用户输入的功能,并且密码列被硬编码为password,因此为了更改它,您还需要在 LoginController 中添加这些功能:
App/Http/Controllers/Auth/LoginController.php
/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function validateLogin(Request $request)
{
$request->validate([
$this->username() => 'required|string',
'user_pass' => 'required|string',
]);
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), 'user_pass');
}
下一步是创建一个自定义 Provider,我们将其命名为 CustomUserProvider,而不是默认的 EloquentUserProvider,您将在其中覆盖密码字段。
App/Providers/CustomUserProvider.php
<?php
namespace App\Providers;
class CustomUserProvider extends EloquentUserProvider
{
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('user_pass', $credentials))) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
if (Str::contains($key, 'user_pass')) {
continue;
}
if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
return $query->first();
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['user_pass'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
}
现在你扩展了默认提供者,你需要告诉 Laravel 使用这个而不是 EloquentUserProvider。这就是你可以做到的。
App/Providers/AuthServiceProvider.php
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
$this->app->auth->provider('custom', function ($app, $config) {
return new CustomUserProvider($app['hash'], $config['model']);
});
}
最后更新配置信息config/auth.php,把驱动从eloquent改成custom(我上面就是这么命名的,你可以随便改)。所以config/auth.php文件应该有这个位:
'providers' => [
'users' => [
'driver' => 'custom',
'model' => App\User::class,
],
],
希望对你有帮助!
问候