API Authentication with Laravel Sanctum: Tokens & SPA Cookies

Modern web systems must authenticate two distinct types of clients: Mobile & Third-Party Applications (which cannot store session cookies and require stateless Bearer API Tokens) and Single Page Applications (SPAs) like React, Vue, or Next.js (which should use secure, HttpOnly CSRF-protected session cookies). Laravel Sanctum is the official lightweight package designed specifically to solve both problems flawlessly.

1. Setting Up Laravel Sanctum

Install and initialize Sanctum using the Artisan command:

# Install API tooling & Sanctum tables
php artisan install:api

# Run migrations to create personal_access_tokens table
php artisan migrate

Add the HasApiTokens trait to your app/Models/User.php model:

namespace App\Models;

use Laravel\Sanctum\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens; // Grants token issuance & inspection methods
}

2. Issuing Mobile / API Bearer Tokens with Abilities

Create an endpoint where mobile users exchange email and password for a long-lived Bearer API token:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Models\User;
use Illuminate\Validation\ValidationException;

Route::post('/api/v1/auth/token', function (Request $request) {
    $request->validate([
        'email'       => 'required|email',
        'password'    => 'required',
        'device_name' => 'required|string',
    ]);

    $user = User::where('email', $request->email)->first();

    if (!$user || !Hash::check($request->password, $user->password)) {
        throw ValidationException::withMessages([
            'email' => ['The provided credentials are incorrect.'],
        ]);
    }

    // Issue token with specific granular abilities (scopes)
    $token = $user->createToken($request->device_name, ['articles:create', 'articles:read']);

    return response()->json([
        'token'      => $token->plainTextToken,
        'token_type' => 'Bearer',
        'user'       => $user,
    ]);
});

3. Protecting API Endpoints with auth:sanctum Middleware

To protect your endpoints, assign the auth:sanctum middleware:

Route::middleware('auth:sanctum')->group(function () {
    // Current authenticated user
    Route::get('/api/v1/user', fn(Request $request) => $request->user());

    // Checking token abilities
    Route::post('/api/v1/articles', function (Request $request) {
        if (!$request->user()->tokenCan('articles:create')) {
            abort(403, 'Your API token lacks permission to publish articles.');
        }
        // Create article...
    });

    // Revoking current device token (Logout)
    Route::post('/api/v1/auth/logout', function (Request $request) {
        $request->user()->currentAccessToken()->delete();
        return response()->json(['message' => 'Token revoked successfully.']);
    });
});

SPA Cookie Authentication Flow (Next.js / React)

For SPAs residing on the same top-level domain as your API (e.g. app.veewom.com communicating with api.veewom.com), Sanctum does not use tokens! Instead, your SPA makes a GET /sanctum/csrf-cookie call first, followed by a standard POST /login with cookies. This provides maximum security against XSS token theft.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Where are Sanctum API tokens stored in the database?

Tokens are stored in the 'personal_access_tokens' table. The token string is hashed with SHA-256 before storage, meaning even if your database is compromised, attackers cannot view the raw plaintext Bearer tokens.

Q2. How does a client pass a Sanctum token in HTTP requests?

Clients attach the token in the Authorization header: 'Authorization: Bearer 1|qW8z...'. Sanctum parses the token ID, locates the hashed digest, and authenticates the user.

Q3. What is the difference between Laravel Sanctum and Laravel Passport?

Sanctum is ultra-lightweight and perfect for mobile apps, SPAs, and basic token auth. Passport is a full OAuth2 server (supporting Authorization Code grants, client credentials, and PKCE) designed for third-party developer platforms like GitHub or Google APIs.

Explore Related Laravel Tutorials & Next Steps

Broaden your backend architecture, REST API design, and enterprise production engineering expertise:

RESTful API Architecture

Protect private API routes with the auth:sanctum middleware pipeline.

API Resources & Serialization

Return authenticated user profiles and tokens cleanly in JSON payloads.

API Token Security & Expiration

Configure Sanctum token expiration timestamps and revoke stolen credentials.

140+ Technical Interview Questions

Explain Sanctum SPA cookie authentication vs Passport OAuth2 in interviews.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!