Laravel interview question · Most asked

Sanctum or Passport: which do you choose for API authentication?

Summary

Sanctum for first-party SPAs (cookie sessions with CSRF protection) and simple API tokens for mobile apps or integrations; Passport when you need a full OAuth2 server for third-party clients.

Longer Answer

Sanctum has two modes. For an SPA on the same top-level domain it uses Laravel's normal session cookie with CSRF protection, so no tokens are stored in JavaScript. For mobile apps and machine clients it issues personal access tokens (hashed in the database) with optional abilities (scopes) and expiry.

Passport implements OAuth2 (authorization code with PKCE, client credentials, refresh tokens). Choose it when other companies' applications need delegated access to your users' data, the "Sign in with your platform" case.

Many teams pick Passport when Sanctum would do; OAuth2 adds complexity you only need for third-party clients.

Key Points

  • Sanctum: SPA cookie auth + simple tokens.
  • Passport: full OAuth2 server.
  • SPA cookie mode avoids tokens in localStorage.

More most-asked Laravel questions

← All Laravel questions