Bagisto Hosting
On this page

Table of Content

API Authentication Using Laravel Sanctum

Hello guys, So in this article, I will show you how to authenticate to API in Laravel using Sanctum. This is very useful when you are building an API for a SPA.

This is a very simple project so follow these steps written below.

#1 Create Laravel project

  •  Okay, then let’s create a fresh new Laravel project.

  • After creating the project, install Laravel Sanctum package inside your project.
Note:- If you are using latest version of laravel then you have to simply migrate your database and start from step: #2 Make Controller and Routes. Here

  • Then publish the Sanctum configuration files. The Sanctum configuration file will be placed inside your config directory.

  • Next, migrate your database.
Note:- Before that provide your database credentials inside your .env file.

  •  In order to use Sanctum we need to use HasApiTokens Trait Class inside our User Model. So that we can provide the token to authenticate user with createToken() method.

So, User model should look like the image below.

#2 Make Controller and Routes

  • So first create a new controller inside App/Http/Controllers/AuthController.php.

  • Update your controls as shown below

  • Here you can notice a trait inside controller. By using this trait we can return a consistent response in a simple way and we don’t need to write same code again and again while returning response.

So, Let’s make a trait inside App/Traits/ApiResponser.php and trait look like shown below.

  • So, Now we have to create routes on routes/Api.php

#3 Finally Test your API

Here we are using postman for API testing please refer the below images for better understanding.

  • First register your account.

  • After registration is successful login to your account.

  • Now try to get all user list from the user table. and don’t forget to send Bearer token in header that you get after login.

  • Now logout if you want.

 

Thank you for reading this tutorial. We hope you found it helpful. If you have any questions or encounter any issues, please feel free to leave a comment below.

Additionally, if you’re looking to hire Laravel developers, you can visit the Hire Laravel Developer page. This platform provides a pool of experienced Laravel developers who can help you with your project requirements and ensure the successful implementation of your ideas.

Furthermore, if you’re interested in enhancing the functionality of Bagisto, you can check out the Extensions page on the official Bagisto website. This page showcases a wide range of extensions that can be integrated into your Bagisto e-commerce platform to add new features, improve user experience, and optimize your online store’s performance.

. . .

Leave a Comment

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


Be the first to comment.

Start a Project




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home