Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeske's avatar
Level 1

Laravel Fortify + Laravel Sanctum

Hey, How can I integrate Laravel Sanctum with Laravel Fortify? So when logging in and using 2FA, a Sanctum token is returned?

0 likes
1 reply
tisuchi's avatar

@jeske Laravel Fortify and Laravel Sanctum are two separate packages that handle authentication in different ways. Fortify is designed to work with traditional, session-based authentication, while Sanctum is designed to work with API token-based authentication.

To integrate the two packages, you would need to modify the login process to handle both the traditional session-based authentication (via Fortify) and the API token-based authentication (via Sanctum) simultaneously.

Here's one way you could do this:

  1. In your login controller, use Fortify's login method to handle the session-based authentication.
  2. In your login controller, after the user is authenticated, use Sanctum's createToken method to generate a token for the user.
  3. Return both the token and the session-based authentication information to the client.
  4. On the client, store the token in local storage or a cookie, and use it for all API requests.
  5. On the client, use the session-based authentication information to authenticate the user for non-API requests.

You would also need to modify your middleware to check for the token on API requests and also check for session-based authentication on web requests

It's also worth noting that Fortify and Sanctum are both built on top of Laravel's built-in authentication features, so you could also customize the authentication guard and provider to handle the authentication process in a way that allows you to use both Sanctum and Fortify together.

Please or to participate in this conversation.