How to get current authenticated user id using Vuejs and Laravel?
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Seamlessly Retrieve Current Authenticated User ID Using Vuejs and Laravel
In today's web development world, integrating frontend frameworks like Vuejs with backend systems such as Laravel has become increasingly common. One of the most essential aspects in these applications is managing user authentication and access control. This blog post will provide a comprehensive guide on how to get the current authenticated user ID using Vuejs and Laravel while adhering to best practices.
Before getting started, ensure you have a basic understanding of Vuejs and Laravel's authentication mechanisms. For a detailed introduction, consider visiting our article at https://laravelcompany.com/blog/vue-and-laravel-authentication/.
Step 1: Enable CSRF Protection in Your Laravel Application
In order to prevent Cross-Site Request Forgery (CSRF) attacks and maintain the security of your application, enable CSRF protection on your Laravel routes. This can be achieved by including the following code snippet in your respective routes file:
```php
Route::middleware('web')->group(function () {
Route::get('/', function () { return view('welcome'); })->name('home');
});
```
Step 2: Setup Laravel's API Resource Routes for Vuejs
Create your resource controller and routes to manage user authentication, which will be used by the Vuejs app. An example of a simple UserController can be as follows:
```php
json([
'user_id' => auth()->user()->id,
]);
}
}
```
Now create an API resource route for retrieving the current authenticated user data:
```php
Route::get('/api/user-data', [UserController::class, 'userData']);
```
Step 3: Access and Display User ID in Vuejs Application
In your Vuejs application, import Axios to handle HTTP requests. Create a component that will display the current logged-in user's id and make an API call at created() lifecycle hook for fetching data.
```javascript
```
Now, you can include this component in your Vuejs app and view the current authenticated user's id. Congratulations! You have successfully retrieved the current logged-in user ID using Vuejs and Laravel.
In conclusion, following these steps will help you seamlessly integrate authentication between your Vuejs and Laravel applications. This approach allows for secure data access while maintaining a clean separation of concerns between frontend and backend systems. For more insight on how to implement various aspects of this technology stack, visit our comprehensive guides at https://laravelcompany.com/blog/.