Maria30's avatar

how can i make login and register links appear in the (navigation) layout of an inertia app with vue and jetstram?

Im trying to make a single layout for both guests and auth users.

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! In a typical Laravel Jetstream + Inertia.js + Vue setup, you can conditionally show login/register or user profile/logout links based on the authentication state of the user. Here’s how you can approach it:

1. Pass Auth State from Laravel to Inertia

In your App\Http\Middleware\HandleInertiaRequests, Jetstream already shares the authenticated user:

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

2. Access Auth State in Your Vue Layout

In your Vue layout component (commonly Resources/js/Layouts/AppLayout.vue), you can now access the auth state via the $page.props.auth.user object.

3. Conditional Rendering in Vue

Modify your navigation like so:

<template>
  <nav>
    <ul>
      <template v-if="!$page.props.auth.user">
        <li>
          <Link href="/login">Login</Link>
        </li>
        <li>
          <Link href="/register">Register</Link>
        </li>
      </template>
      <template v-else>
        <li>
          Hello, {{$page.props.auth.user.name}}
        </li>
        <li>
          <Link href="/logout" method="post" as="button">Logout</Link>
        </li>
      </template>
    </ul>
  </nav>
</template>

<script setup>
import { Link } from '@inertiajs/vue3'
</script>

4. Use the Layout

Make sure all your pages use this layout.


That’s it! Now, when a user is not authenticated, Login and Register show up. When logged in, the user’s name and a Logout button appear instead. Let me know if you need to customize the user object or links further!

Please or to participate in this conversation.