在登录过程中添加操作 [英] Add action during login process

查看:66
本文介绍了在登录过程中添加操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Laravel7.x.应用程序,它将结合标准身份验证(用户通过表单登录)和通过Sanctum(基于令牌)的API身份验证.我想在成功的用户身份验证期间生成一个Sanctum API令牌.为此,我需要加入登录流程.

I am building a Laravel 7.x. application that will combine both standard authentication (user logs on via form) and API authentication through Sanctum (token based). I want to generate a sanctum API token during successful user authentication. To do that, I need to hook into the login flow.

通过运行 php artisan ui vue --auth ,标准身份验证被应用到我的应用程序中.

Standard authentication was scaffolded to my application by running php artisan ui vue --auth.

当我检查 routes/web.php 时,我只能看到 Auth :: routes();

When I inspect routes/web.php, I can see only Auth::routes(); which under the hood allegedly generates the classic routes I was used to in previous Laravel versions. Taken from the answer I linked, /login route definition that is generated looks like this:

$this->post('login', 'Auth\LoginController@login');

但是,当我检查脚手架的 LoginController 时,看不到 Auth :: routes()应该生成的任何方法.那里什么也没有,对于开发人员来说,一切似乎对我都是透明的:

However, when I inspect my LoginController that was scaffolded, I can not see any of the methods that should be generated by Auth::routes(). There is nothing in there and it looks like everything is handled transparently to me as a developer:

class LoginController extends Controller
{
    use AuthenticatesUsers;
    
    protected $redirectTo = RouteServiceProvider::HOME;
    
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

我如何加入登录流程并向其中添加自己的操作?

How do I hook into the login flow and add my own actions to it?

推荐答案

我可以看到,这里有两个问题,我将尽力回答.

I can see, two questions here and I will try to answer them as good as I can.

1.对成功的用户身份验证执行操作

我认为实现这一目标的最干净的方法是利用Laravel事件/侦听器体系结构.

I think the cleanest way to achieve this, it to utilize the Laravel event / listeners architecture.

app/Providers/EventServiceProvider.php 中扩展 $ listen 数组

// include at the top
use Illuminate\Auth\Events\Login;

protected $listen = [
        // other handlers [],
    Login::class => [
        CreateUserApiToken::class,
    ],
];

然后执行此artisan命令,该命令将神奇地创建您的侦听器文件

Then execute this artisan command, which will magically create your listener file

php artisan event:generate

现在,您可以打开 app/Providers/CreateUserApiToken.php 并将所需的内容放在 handle 函数中.

Now you can open app/Providers/CreateUserApiToken.php and put whatever you like in the handle function.

public function handle(Login $event)
{
    dd($event->user);
}

2.实际的laravel代码在哪里?

对于许多Laravel类,您只会找到直接投影到您的应用程序目录中的少量代码.它的大多数隐藏在特征或扩展父类中.让我们以 LoginController 为例

For lots of Laravel classes, you will only find a minimal amount of code projected directly to your app directory. Most of it is hidden in traits or by extending parent classes. Let's take the LoginController for example

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use AuthenticatesUsers;

这是控制器正在使用的特征.特别是最上面的一行为您提供了该文件所在位置的很好的线索.您还可以使用代码编辑器搜索功能,在所有文件中搜索 trait AuthenticatesUsers .

This is the trait that controller is using. And especially the top line gives you a pretty good clue where that file is located. You could also use your code editors search function, to search in all files for trait AuthenticatesUsers.

在这种情况下,相应的文件将是 vendor/laravel/ui/auth-backend/AuthenticatesUsers.php .在这里,您将找到所需的大多数功能.当然,直接覆盖该文件不是一个好主意,因为如果laravel框架得到更新,所有更改都将丢失.

In this case the corresponding file would be vendor/laravel/ui/auth-backend/AuthenticatesUsers.php. Here you will find most of the functions that you are looking for. Of course, it's not a good idea to overwrite that file directly, because all the changes would be lost, if the laravel framework get's updated.

但是,如果在其中找到要更改的函数,例如 showLoginForm(),则可以将该函数简单地包含在LoginController中并更改代码.

But if you find a function in there, that you want to change, let's say showLoginForm() for example, you can simply include that function in your LoginController and change the code.

public function showLoginForm()
{
    return view('example.login');
}

这篇关于在登录过程中添加操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆