Laravel implements simulated user login

小云云
Release: 2023-03-22 12:04:02
Original
2289 people have browsed it

This article mainly shares with you Laravel to implement simulated user login, hoping to help everyone.

//强制用户id位100的人登录Auth::loginUsingId(100);//获取这个登录用户的id;dd(auth()->user()->id);
Copy after login

During testing, I need to test the relevant information of a certain logged-in user. We can force login in the auth provider. In this case, there is no need to write a loginUsingId in a specific method. .

app\Providers\AuthServiceProvider.php
Copy after login

Add the user you want to log in to the boot, then the user will be logged in globally.

    public function boot()
    {
        auth()->loginUsingId(348);        $this->registerPolicies();
    }
Copy after login

After the test is completed, just remove that line. If you want to log in somewhere In this method, temporarily change the user login, then use loginUsingId again.

Related recommendations:

Code to use curl to simulate user login under PHP_PHP tutorial

The above is the detailed content of Laravel implements simulated user login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!