Guides · SaaS

Laravel Sanctum vs Passport: which API authentication should you choose?

Sanctum or Passport for a Laravel API? Differences in tokens, SPA cookies, OAuth2, mobile apps and complexity, with a simple decision guide and common mistakes.

Key takeaways

  • Sanctum is the simple choice: API tokens for mobile apps and cookie-based sessions for your own single-page app.
  • Passport is a full OAuth2 server. Use it only when third parties need to authorise against your API.
  • Most small and mid-size products need Sanctum.
  • Whichever you use, serve everything over HTTPS and keep tokens revocable.

The question to ask first

Who is calling your API?

  1. Your own web app on the same domain or a trusted subdomain.
  2. Your own mobile app.
  3. Other companies' apps, acting on behalf of your users.

The first two are Sanctum territory. The third is where OAuth2, and therefore Passport, comes in.

Sanctum

Sanctum offers two modes.

Cookie authentication for SPAs. Your front end first requests a CSRF cookie, then logs in. After that, the browser sends a session cookie, with the usual protections. There are no tokens to store in JavaScript.

API tokens. A user (or your mobile app after login) creates a personal access token. It is sent as Authorization: Bearer <token>. Tokens can have abilities, a rough form of scopes.

Creating a token after a successful login:

$token = $user->createToken('android-app', ['read', 'write'])->plainTextToken;

Protect routes:

Route::middleware('auth:sanctum')->get('/me', fn (Request $r) => $r->user());

Passport

Passport adds a complete OAuth2 server: authorisation codes, client credentials, refresh tokens and scopes. It is what you need if outside developers register apps that request access to your users' data, as large platforms allow.

It is more to install, configure and maintain, and OAuth2 has many flows that are easy to misuse. Do not take that on without a requirement.

Decision guide

SituationChoose
Your own Vue or React SPASanctum (cookies)
Your own Android or iOS appSanctum (tokens)
Simple server-to-server integration with one partnerSanctum tokens, or a signed-request scheme
Third-party developers building on your platformPassport

Mobile app tips

  • Store the token in secure storage on the device, not plain preferences.
  • Offer logout that deletes the token on the server, not only on the phone.
  • Allow users to list and revoke devices.
  • Handle 401 responses by returning the user to the login screen.

Common mistakes

  • Putting tokens in URLs. They end up in logs.
  • Tokens that never expire. Set an expiry or rotate them.
  • Skipping rate limits on login and OTP routes. Use Laravel's rate limiting.
  • Returning more data than the screen needs. Use API resources.
  • Using HTTP in production.

Frequently asked questions

Can I switch from Sanctum to Passport later?

Yes, but clients will need to change how they authenticate. Choose based on the audience you expect.

What about JWT?

JWT packages exist and are common, but they are not necessary for most Laravel projects. Sanctum covers typical needs with revocable tokens.

Is OTP login compatible with Sanctum?

Yes. Verify the OTP, then issue a token or log the user in. The authentication method is independent of the token system.

One Laravel backend can serve a website, an Android app and an admin panel.

See Score Delta