Protect an Inertia page with auth middleware

auth on the Laravel route is what sends a guest to login. A v-if on the Vue page does not.

For your AI agent

Copies a prompt. Paste it into the AI agent you already have open.

Read https://laravel-vuejs.space/auth-middleware-inertia-vue.md
auth on the Laravel route is what sends a guest to login. A v-if on the Vue page does not.
Do not fetch /posts with fetch() and expect the login redirect to become JSON.
Do not add Vue Router guards as a second copy of this middleware.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Inertia auth middleware” hide the nav when auth.user is null and leave the route open. Anyone can still request it. Put the middleware on the route. The setup guide shares auth.user for the menu. The menu is not the lock.

TL;DR

  • auth on the Laravel route is what sends a guest to login.
  • A v-if on the Vue page does not.
  • Do not fetch /posts with fetch() and expect the login redirect to become JSON.
  • Do not add Vue Router guards as a second copy of this middleware.

Who it's for: Laravel + Vue developers on Inertia

Applies to: Laravel 13 · Inertia 3 · Vue 3

The fix#

php
Route::get('/posts', [PostController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('posts.index');

A guest is redirected to login. Inertia follows that redirect, so the Vue page never mounts. You do not handle this in router.on('error').

Use the prop only for presentation:

vue
<p v-if="page.props.auth.user">{{ page.props.auth.user.name }}</p>

What to avoid#

Gotcha

Do not fetch /posts with fetch() and expect the login redirect to become JSON. An Inertia visit understands the redirect. A hand-rolled request gets the login HTML. Do not add Vue Router guards as a second copy of this middleware. The SPA guide is the only place a client guard belongs, and only on that separate app.

FAQ#

How do I protect an Inertia page with auth middleware?

auth on the Laravel route is what sends a guest to login. A v-if on the Vue page does not.

What goes wrong with Inertia auth middleware?

Many developers hide the nav when auth. Do not fetch /posts with fetch()` and expect the login redirect to become JSON.

Found this useful?

Ask questions, share what you built.

Join thousands of Laravel and Vue developers in our Facebook group, or get new posts in your Telegram feed. Sharing this article helps too.

Keep reading

Newsletter

Get the best of Laravel & Vue, once a month.

One short email with new guides, release notes worth reading and hand-picked community links. No spam, unsubscribe any time.