简体   繁体   English

如何覆盖 Laravel 中的身份验证登录功能

[英]How to override auth login function in Laravel

I need to override laravel's default login function, however I am confused by the code I find there.我需要覆盖 laravel 的默认登录功能,但是我对我在那里找到的代码感到困惑。 I do not understand how laravel AuthenticatesUsers.php verifies that user exists in db and then authenticates the user.我不明白 laravel AuthenticatesUsers.php 如何验证用户是否存在于 db 中,然后对用户进行身份验证。

I've come to this function in AuthenticatesUsers.php我在AuthenticatesUsers.php 中使用了这个函数

/**
 * Attempt to log the user into the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return bool
 */
protected function attemptLogin(Request $request)
{
    return $this->guard()->attempt(
        $this->credentials($request), $request->filled('remember')
    );
}

I guess I would need to override this function inside auth/LoginController.php我想我需要在 auth/LoginController.php 中覆盖这个函数

but I do not understand how it works.但我不明白它是如何工作的。 Where are guard() and attempt() implemented, I guess the real action happens there.在哪里实现了guard()attempt() ,我猜真正的动作发生在那里。 I need to add another condition to the login.我需要为登录添加另一个条件。

Anyone understands how laravel's default login works?有人了解laravel的默认登录是如何工作的吗? and those two functions guard() and attempt() , for example attempt is not implemented inside AuthenticatesUsers.php itself and I believe its this function I should really override.以及这两个函数guard()attempt() ,例如尝试在AuthenticatesUsers.php 本身内部没有实现,我相信它的这个功能我真的应该覆盖。

Where can I find it's implementation?我在哪里可以找到它的实现?

I would suggest not overriding any functions, instead create a new guard and provider .我建议不要覆盖任何函数,而是创建一个新的guardprovider Look at the config/auth.php to see how they are defined.查看config/auth.php以了解它们是如何定义的。

Then you can set your custom guard as default and the normal auth flow will take over.然后,您可以将自定义保护设置为默认值,然后正常的身份验证流程将接管。

See the documentation on Adding Custom Guards请参阅有关添加自定义防护的文档

I have just changed HomeController.php - using 'guest' as the middleware instead of 'auth'.我刚刚更改了 HomeController.php - 使用“guest”作为中间件而不是“auth”。 For anyone in a similar situation, this works as a temporary solution.对于处于类似情况的任何人,这都可以作为临时解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM