Add action during login process

旧街凉风 提交于 2021-02-08 07:51:49

问题


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.

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

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');

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?


回答1:


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

1. Execute action on successful user authentication

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

In app/Providers/EventServiceProvider.php extend the $listen array

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

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

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

php artisan event:generate

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. Where's the actual laravel code?

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;

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.

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.

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');
}


来源:https://stackoverflow.com/questions/63058533/add-action-during-login-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!