Home > Web Development > Laravel Tutorials > Protect your REST API easily with Laravel Sanctum.

Protect your REST API easily with Laravel Sanctum.

Diego Cortés
Diego Cortés
January 21, 2025
Protect your REST API easily with Laravel Sanctum.

In the world of modern web development, API security is a paramount topic. With the rise of mobile applications and service integration, protecting the communication between your application and users becomes essential. Today, we present a simple and effective solution to secure your REST API using Laravel Sanctum.

What is Laravel Sanctum?

Laravel Sanctum is a lightweight authentication package designed specifically for simple applications or SPAs (Single Page Applications) that need to authenticate users easily. Unlike Laravel Passport, which is more complex and geared towards applications that require OAuth2, Sanctum is ideal for developers looking for a quick and easy-to-implement alternative.

Advantages of Using Laravel Sanctum

Quick Implementation

One of the main advantages of Laravel Sanctum is its ease of implementation. There is no need for complicated configurations. With just a few commands, you can have a functional and secure solution to protect your REST API.

Compatible with Both SPAs and Traditional APIs

Sanctum allows for seamless authentication for both SPA applications and traditional APIs. This means you can use the same authentication system across different types of applications, making your work more efficient and organized.

Versatility

Sanctum supports a variety of authentication methods, such as API tokens and cookies. This provides you with the flexibility to choose the method that best suits your needs.

How to Integrate Laravel Sanctum into Your Project

Installation

To start using Laravel Sanctum, you first need to install it in your project. You can do this by running the following command in your terminal:

composer require laravel/sanctum

Publish Configuration

After installation, you need to publish the package's configuration with the following command:

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

Migrations

Make sure you have created the necessary migrations for the access tokens table. You can do this by running:

php artisan migrate

Middleware Configuration

Next, you need to ensure that your middleware is configured correctly. Open the api.php file and add the auth:sanctum middleware to your routes that require authentication.

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

Token Generation

Once you have completed the above steps, you can start generating access tokens. This can be done directly from your User model. For example:

use Illuminate\Support\Facades\Hash;

public function createToken($name) {
    return $this->tokens()->create([
        'name' => $name,
        'token' => Hash::make(Str::random(60)),
    ]);
}

Conclusion

Laravel Sanctum presents itself as an efficient and quick solution to protect your REST API, simplifying the authentication process without sacrificing security. This tool is perfect for developers who seek agility in their projects without unnecessary complications.

If you want to stay updated on more articles like this, I invite you to keep exploring the content of my blog. Until next time!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 22.92 ms