Issue a Sanctum token without breaking the SPA

A first-party Vue SPA uses the session cookie. createToken is for a mobile client or another product.

For your AI agent

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

Read https://laravel-vuejs.space/sanctum-api-token-beside-spa.md
A first-party Vue SPA uses the session cookie. createToken is for a mobile client or another product.
Do not put the token in localStorage on the Vue starter kit, and do not send Authorization: Bearer from an Inertia form.
Show the diff. Do not commit.
On this page · 3 sections
  1. The fix
  2. What to avoid
  3. FAQ

People searching “Sanctum API token Vue” store a bearer token in the Inertia app. Sanctum’s own guidance, written up in the Sanctum guide, is that your own SPA uses the session cookie. A token is for a client that cannot use that cookie.

TL;DR

  • A first-party Vue SPA uses the session cookie.
  • createToken is for a mobile client or another product.
  • Do not put the token in localStorage on the Vue starter kit, and do not send Authorization: Bearer from an Inertia form.

Who it's for: Laravel + Vue developers

Applies to: Laravel 13 · Vue 3 · Sanctum

The fix#

The user model uses Sanctum’s trait, not Passport’s. The Passport comparison is why those two traits do not stack.

php
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
}

Issue a token from a route the mobile client calls after it has authenticated. Return the plain text once:

php
$token = $request->user()->createToken('phone')->plainTextToken;

return response()->json(['token' => $token]);

The Inertia pages keep using auth and the session. They never read this JSON.

What to avoid#

Gotcha

Do not put the token in localStorage on the Vue starter kit, and do not send Authorization: Bearer from an Inertia form. You would be maintaining two logins for one browser.

FAQ#

How do I issue a Sanctum token without breaking the SPA?

A first-party Vue SPA uses the session cookie. createToken is for a mobile client or another product.

What goes wrong with Sanctum API token Vue?

Many developers store a bearer token in the Inertia app. Do not put the token in localStorage on the Vue starter kit, and do not send Authorization: Bearer from an Inertia form.

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.