在Laravel HTTP基本身份验证实现中,它使用电子邮件作为用户名,如何将列更改为用户名?

时间:2019-04-22 14:18:37

标签: laravel httprequest laravel-authentication

laravel中,我们可以使用Http Basic Authentication来方便用户login。通过将middleware->('auth.basic')附加到Routeweb.php的末尾而没有登录页面,但是默认情况下,它将电子邮件作为用户名,我该如何更改? 谢谢。

Route::get('/somePage','Controller@controllerFunction')
->middleware('auth.basic');

图片: https://drive.google.com/file/d/1gbzE0azW8TfZsvQN7k7ZoG2PIRlo14i6/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

来自docs

  

将中间件附加到路由后,您将   在访问以下路径时,系统会自动提示您输入凭据   您的浏览器。默认情况下,auth.basic中间件将使用电子邮件   用户记录上的“用户名”列。

默认中间件内部有一个handle()方法。

您必须创建自己的中间件并覆盖handle()

namespace app\Http\Middleware;

use Closure;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;

class YourBasicAuthMiddleware extends AuthenticateWithBasicAuth
{
    public function handle($request, Closure $next, $guard = null, $field = null)
    {
        $this->auth->guard($guard)->basic($field ?: 'YOUR_FIELD'); //place here the name of your field

        return $next($request);
    }
}

比更新您的App\Http\Kernel

'auth.basic' => \app\Http\Middleware\YourBasicAuthMiddleware::class,