1
votes

I have a Laravel 8 application that uses Fortify with custom views to login and register users. I have some API routes that I want to get the logged-in user details on them as well. The problem is that I don't want to use Passport and other packages to make the job hard to generate tokens and so on because most of the routes are in the web.php and just some routes are on api.php, so what I have tried is like below :

dd(auth()->guard('api')->user());

However, it returns null and...

dd(Auth::user());

Again returns null. Is there any way to get the authenticated user in the API routes?

1

1 Answers

1
votes

In your controller method, you can retrieve the user with these two methods:

public function render(Request $request) {
    $user = $request->user(); // 1
    $user = auth()->user(); // 2
}