Web Prepration

Web Concept Tutorials

Laravel, Laravel 7, Laravel 8

In this post, I am going to explain how to use sanctum api in laravel 8. Laravel 8 Sanctum provides a simple authentication system for SPAs (Single page applications), mobile applications token based APIs.

Sanctum API allows each user of your application to generate multiple API tokens for their account. You can create mobile based API using sanctum, its provide fully restful api and for use in laravel to follow basic steps which is given below.

Step 1: Install Laravel 8

In this step I am going to explain how to install fresh laravel 8, If you have already istalled then ignore installation steps, I am going to explain from scratch so, we need to get fresh Laravel 8 application using bellow command, So open your terminal OR command prompt and run bellow command:

Step 2: Install Sanctum

In this step we need to install sanctum via the Composer package manager, so open your terminal and fire bellow command:

After successfully installation of package, we need to publish configuration file for that just use below command.

we require to get default migration for create new sanctum tables in our database. so let’s run bellow command.

Next, we need to add middleware for sanctum api, so let’s add as like bellow:

app/Http/Kernel.php

Step 3: Sanctum Configuration

In this step, we need to configuration on three place model, service provider and auth config file.

In model we added HasApiTokens class of Sanctum,

In auth.php, we added api auth configuration.

app/Models/User.php

Step 4: Add Post Table and Model

Now, we need to create migration for posts table using Laravel 8 php artisan command, so first fire bellow command:

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create posts table.

After create migration we need to migrate for creating database table for posts using migrate command.

app/Models/Post.php

Step 5: Create API Routes

In this step, we will create api routes. Laravel provide api.php file for write web services route. So, let’s add new route on that file.

routes/api.php

Step 6: Create Post Controller Files

In this step, now we need to create new controller as BaseController, PostController and RegisterController, i created new folder “API” in Controllers folder because we will make alone APIs controller, So let’s create both controller:

app/Http/Controllers/API/BaseController.php

app/Http/Controllers/API/RegisterController.php

app/Http/Controllers/API/PostController.php

Step 7: Create Eloquent API Resources

This is a very important step of creating rest api in laravel 8. you can use eloquent api resources with api. it will helps you to make same response layout of your model object. we used in PostController file. now we have to create it using following command:

app/Http/Resources/Post.php

Now we are ready to to run full restful api and also passport api in laravel. so let’s run our example so run bellow command for quick run:

I hope you found your best…

0 comment on How to use Sanctum API Authentication in Laravel

Write a comment

Your email address will not be published. Required fields are marked *