4

I'm using laravel and inertia.js to implement my project. In my navbar I want to display div element with some user details if the user is logged in. And if the user is not logged in the div should not appear. I have tried this but its not showing the details neither when I am logged in nor when I'm not. What should I do?

<div class="ml-3 relative" v-if="$page.props.auth.user">
    <div>
        <button @click="dropDown=true"
                class="bg-gray-800 flex text-sm rounded-full focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-white"
                id="user-menu" aria-haspopup="true">
            <span class="sr-only">Open user menu</span>
            <img class="h-8 w-8 rounded-full object-cover" :src="$page.props.user.profile_photo_url"
                 :alt="$page.props.user.name"/>
        </button>
    </div>
    <div v-if="dropDown"
         class="origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1 ring-black ring-opacity-5"
         role="menu" aria-orientation="vertical" aria-labelledby="user-menu">
        <a href="#" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" role="menuitem">
            <form method="POST" @submit.prevent="logout">
                <jet-responsive-nav-link as="button">
                    Logout
                </jet-responsive-nav-link>
            </form>
        </a>
    </div>
</div>

On AppServiceProvider

public function boot()
{
    //check if user is logged in
    Inertia::share('auth.user', function() {
        return ['loggedIn' => Auth::check()];
    });
}

5 Answers 5

8

With Inertia you can create a HandlerInertiaRequest Middlware. The HandleInertiaRequests middleware provides a "share" method where you can define the data that is automatically shared with each Inertia response. In Your case thats the user which is logged in.

You can find more infos in the docs Shared Data. They have an example in there demo app repository.

In your view you can check if the user is logged in by checking the shared data user prop.

v-if="$page.props.auth.user"
Sign up to request clarification or add additional context in comments.

Comments

5

Laravel 8.4.0 with InertiaJS returns $page.props.user by default if user logged in (see this answer). So you can simply do,

<element v-if="$page.props.user">This will only show when a user logged in.</element>

For newer versions, this answer here is a better practice.

Comments

3

Installation of inertiajs adds a HandleInertiaRequests middleware for the $middlewareGroups web group.

In the share method of this middleware You can define a property in which the boolean value of the user's authorization status in the system will be written.

For example:

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'ziggy' => function () use ($request) {
            return array_merge((new Ziggy)->toArray(), [
                'location' => $request->url(),
            ]);
        },
        'auth.user.check' => fn () => (bool) $request->user(),
    ]);
}

Further in your template and for all pages for which the user authorization check should be performed, you must define a constant, for example isAuth.

* You can read the property passed from the Middleware as follows: page.props.auth.user

<script setup>
import { computed } from 'vue'
import { Link, usePage } from '@inertiajs/vue3'

const page = usePage()
const isAuth = computed(() => page.props.auth.user)

</script>

In template tag:

// ......
<Link v-if="isAuth" :href="route('profile')">Profile</Link>
<Link v-if="!isAuth" :href="route('login')">Login</Link>
// ......

Thus, you can define any parameters that should be globally available in your application or should be used on many (most) pages.

Documentation: https://inertiajs.com/shared-data.

Checked work: Laravel 10 + inertia-laravel v.0.6.9

Comments

2

I guess you can follow the same approach built here in this middleware , https://github.com/inertiajs/pingcrm/blob/master/app/Http/Middleware/HandleInertiaRequests.php and then you separated the logic from the providers

Comments

0

Open this file : app\Http\Middleware\HandleInertiaRequests.php

In this file you can find a method called share and inside this method you can return anything you want. For example you can return an instance of current user

public function share(Request $request): array
    {
        $user = auth()->user();
        return array_merge(parent::share($request), [
            'user' => $user
        ]);
    }

And now inside your component, in data section, you can access user variable like this:

data : function () {
            return {
                user : this.$page.props.user
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.