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

beracah.kings's avatar

Sanctum vs Passport

We have a laravel application . Client wants to change the app to be api driven with React FrontEnd (for Web) and React Native (for Mobile) . Is it ok to use sanctum for authentication. Could anyone advise please

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, Sanctum can be used for authentication in a Laravel application that is API-driven with React frontend and React Native for mobile. Sanctum is a lightweight authentication system that is easy to set up and use. It provides a simple way to authenticate users and issue API tokens.

Sanctum is a good choice for applications that do not require complex authentication requirements. If your application requires more advanced authentication features such as OAuth2, then Passport may be a better choice.

Here is an example of how to use Sanctum for authentication in a Laravel application:

  1. Install Sanctum using composer:
composer require laravel/sanctum
  1. Publish the Sanctum configuration file:
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
  1. Run the Sanctum migration:
php artisan migrate
  1. Add the Sanctum middleware to your API routes:
Route::middleware('auth:sanctum')->group(function () {
    // API routes here
});
  1. To authenticate a user and issue an API token, use the createToken method:
$user = User::find(1);
$token = $user->createToken('token-name')->plainTextToken;
  1. To authenticate a user using an API token, send the token in the Authorization header:
Authorization: Bearer {api-token}

For more information on using Sanctum, see the Laravel documentation: https://laravel.com/docs/8.x/sanctum

Please or to participate in this conversation.