John Davidson

php - Laravel Auth()->user()->id is null when I try to call API

0 comments
Message:


Sorry, I'm still new to Laravel.
I have a Laravel app, that has an API route, which should ask for the Timer, of the logged in person, however it always returns null.


This is my App\Http\Controllers\Api\TimerController.php


public function index(){
dd(Auth()->user()->id);
return Timer::where('userID', Auth()->user()->id)->first();
}

My routes\api.php


Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::get('timer', 'App\Http\Controllers\Api\TimerController@index');

And after this I have a vue frontend application that processes the data it receives.
However I really do only want to give through the API the logged in persons data.
Am I missing something, that prevents me from checking if I'm logged in or not? Because in the other Controllers I can check the login of a person with Auth()->user()->id, but not in this one.



My Answer:
Laravel Auth() is a built-in authentication system in Laravel, a popular PHP framework. It provides a simple way to authenticate users and manage user sessions in your web application.

With Laravel Auth(), you can easily create login and registration functionality, password reset, email verification, and more. It also provides middleware to protect routes and ensure that only authenticated users can access certain parts of your application.

To use Laravel Auth(), you can simply run the `php artisan make:auth` command in your terminal, which will generate all the necessary files and routes for authentication. You can then customize the authentication views and functionality to fit your application's needs.

Overall, Laravel Auth() is a powerful and convenient tool for handling user authentication in Laravel applications.

Rate this post

3 of 5 based on 8298 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us