Laravel 9 Sanctum Authentication

Laravel 9 Sanctum Authentication

In this post we will see how to create a restful api in laravel 9 with authentication applying the sanctum package, using models, seeds, migrations, factories

Laravel 9 Sanctum Authentication

First we will start by creating a new project with composer the dependency manager

composer create-project laravel/laravel laravel-api

Once the project has been created with the artisan command tool, we proceed with creating a controller of type resource

php artisan make:controller ProjectController --resource

Then we will create a new model, we enter the following command in the terminal

php artisan make:model Project -m

We continue with the migrations in laravel framework, with their respective table

php artisan make:migration create_projects_table --create=projects

We add the provider configurations in sanctum

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

Before continuing with the configurations, create a new database, you can review this article on how to create tables in mysql?

We proceed to execute all the migrations that we have created, including the default migration of the users

php artisan migrate

In the DatabaseSeeder.php file we add the number of records that we believe are relevant

\App\Models\Project::factory(1)->create();
\App\Models\User::factory(1)->create();

We execute all the seeds that we have created in our application to test

php artisan db:seed

It is necessary to create a controller for the login with sanctum, using the postman tool, we have to add a parameter of type string in the createToken() method, because if it does not throw us an error

$token = $request->user()->createToken('test')->plainTextToken;

We show the result of the query in json format, with the response class


return response()->json([
'token' => $token,
'email' => $request->user()->email,
'message' => 'Success'
]);

We will create a route for login and authentication, remember to import the corresponding namespaces at the top

route::post('login',[LoginController::class,'login'])->name('login');

We continue creating a resource type route, associated to the project controller, making use of the middleware

Route::apiResource('projects', ProjectController::class)
->middleware('auth:sanctum');

In our project controller, we add the following code

$projects = Project::all();
return response()->json($projects);

It is time to log into the application to obtain the respective access token

Login Laravel 9

Once with the token generated, it is necessary to add it to the Bearer Token to make requests to our rest api in Laravel 9

Bearer Token Laravel 9

We verify the routes that we have created with the following command

php artisan route:list

Now we make a request to one of our endpoints, which shows us a response with status code 200

Laravel 9 Petición Post

This was a way to create a restful api in simple steps using laravel framework version 9

Author