Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the coder-elementor domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rocket domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u262393194/domains/codestap.com/public_html/wp-includes/functions.php on line 6114
Explain how to use Laravel Sanctum for API authentication. - Code Stap
Explain how to use Laravel Sanctum for API authentication.

Explain how to use Laravel Sanctum for API authentication.

Laravel Sanctum provides lightweight API authentication for SPAs (Single Page Applications), mobile apps, and simple token-based APIs. Here’s how to set up and use Sanctum for API authentication in a few steps:

1. Install Sanctum

Run the following command to install Laravel Sanctum via Composer:

Example

composer require laravel/sanctum

2. Publish Sanctum Config

Publish the Sanctum configuration file by running:

Example

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

3. Run Sanctum Migrations

Sanctum requires a database table to store API tokens. Run the migration:

Example

php artisan migrate

4. Add Sanctum Middleware

To protect routes, add Sanctum’s middleware in app/Http/Kernel.php under the api guard:

Example

<?php
'api' => [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],
?>

5. Issue API Tokens

In your controller, use the createToken() method to generate a token for a user.

Example

<?php
$user = User::find(1);
$token = $user->createToken('API Token')->plainTextToken;

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

6. Protect API Routes

Use the auth:sanctum middleware to protect API routes that require authentication.

Example

<?php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});
?>

7. Send Token with API Requests

Send the token in the Authorization header of your API requests:

Example

Authorization: Bearer <your-token-here>

Related Questions & Topics