Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

arrrssssss's avatar

Auth User Data in Laravel Vue Inertia App

Hi everyone,

Recently I'm working on a laravel + vue + inertia app, in this app I have components to create modal for user auth (register, login and ...) that contact with backend (laravel) via axios, right now I need a good approach about accessing authenticated user,

  1. Inertia have its own page.props that stores auth.user data, so in any axios response I can fill or change value on inertia props, and its accesible via every componenets

  2. There is a solid option like pinia, to access data in all of components

which would be the best choice in your idea and experiences?

hope if you have any further information share with me

Regards

0 likes
2 replies
LaryAI's avatar
Level 58

In a Laravel + Vue + Inertia application, managing authenticated user data efficiently is crucial for a seamless user experience. Both options you mentioned—using Inertia's page.props and a state management library like Pinia—are valid, but they serve slightly different purposes and have their own advantages.

Option 1: Using Inertia's page.props

Advantages:

  • Simplicity: Inertia automatically shares data with your Vue components through page.props. This means you can easily access the authenticated user data without additional setup.
  • Consistency: Since Inertia handles the data sharing, you ensure that the data is always up-to-date with the server state.
  • No Additional Dependencies: You don't need to introduce another library, which keeps your application lightweight.

Implementation: To use Inertia's page.props, you can share the authenticated user data from your Laravel backend like this:

// In your AppServiceProvider or a dedicated middleware
use Inertia\Inertia;

public function boot()
{
    Inertia::share([
        'auth' => function () {
            return [
                'user' => auth()->user(),
            ];
        },
    ]);
}

In your Vue components, you can access the user data like this:

<template>
  <div>
    <p>Welcome, {{ $page.props.auth.user.name }}</p>
  </div>
</template>

<script>
export default {
  // Your component logic
}
</script>

Option 2: Using Pinia

Advantages:

  • Centralized State Management: Pinia provides a centralized store for managing state across your application, which can be beneficial if you have complex state management needs.
  • Reactivity: Pinia's state is reactive, which means any changes to the state are automatically reflected in your components.

Implementation: To use Pinia, you would first set up a store for your user data:

// store/user.js
import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    user: null,
  }),
  actions: {
    setUser(userData) {
      this.user = userData;
    },
  },
});

Then, in your Vue components, you can use the store to access and update the user data:

<template>
  <div>
    <p>Welcome, {{ userStore.user.name }}</p>
  </div>
</template>

<script>
import { useUserStore } from '@/store/user';

export default {
  setup() {
    const userStore = useUserStore();

    // Example of setting user data after an API call
    // axios.get('/api/user').then(response => {
    //   userStore.setUser(response.data);
    // });

    return { userStore };
  },
}
</script>

Conclusion

  • Use Inertia's page.props if you want a straightforward approach that leverages Inertia's built-in capabilities for sharing data between the server and client.
  • Use Pinia if you need more advanced state management features, especially if your application has complex state requirements beyond just user authentication.

Ultimately, the choice depends on the complexity of your application and your specific needs. For many applications, Inertia's page.props will be sufficient and simpler to implement.

Please or to participate in this conversation.