Skip to content

Learn how to create token based authentication for REST API using Laravel Sanctum. This project provides an API endpoint for registering, logging in, logging out, and getting data which is the implementation of one of Laravel Sanctum's uses, namely the API Token

Notifications You must be signed in to change notification settings

gustonecrush/api-token-laravel-sanctum

Repository files navigation

DOCUMENTATION

LIST OF CONTENT

ABOUT

Learn how to create token based authentication for rest api using laravel sanctum. This project provides an API endpoint for registering, logging in, logging out, and getting data which is the implementation of one of Laravel Sanctum's uses, namely the API Token. In the HTTP Request process for the registration and login process, the system will generate an api token. This API token will later be used for the process of retrieving user data and also the logout process.

HOW TO CREATE PROJECT

SET UP PROJECT

  • Download this project or clone this repo and save to your local
  • Run composer install to install all dependencies needed, if you have not the composer you can go to here to see how to install it
  • Download and install Laravel in your computer, go to here to see how to install it
  • Create new laravel project, see the documentation
  • Open your computer server to run your server, then create new database on your database
  • Configure your .env file, go to database section, and configure the database according to the database you have made before, the user, and the password you are using as below. If you don't make database yet, so make new database in your server. Then configure in .env file.
        DB_CONNECTION=mysql
        DB_HOST=your_host
        DB_PORT=your_port
        DB_DATABASE=your_db_name
        DB_USERNAME=your_username
        DB_PASSWORD=your_password
    

SET UP LARAVEL SANCTUM PACKAGE

  • You can first check in the composer.json file whether laravel sanctum has been installed or not in your laravel project
  • If there is no laravel sanctum so run command below
      composer require laravel/sanctum
    
  • After that, we publish the laravel sanctum configuration and also the migration file using the command
      php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
    
  • Next we run migrations
      php artisan migrate
    

CREATE AUTH CONTROLLER

  • Reopen the terminal, then run the following command
      php artisan make:controller Api\AuthController
    
  • After we run the command, we can see that there is a new file, namely AuthController.php in the app/Http/Controllers/Api directory. Open the AuthController.php file, then we add register(), login(), and logout() methods. You can see how the code run in this folder app\Http\Controllers

DEFINE THE ROUTE

  • The next step is to define the route. It's different from the usual tutorial, because this tutorial is about api, so we define routes in the routes/api.php file. Open the routes/api.php file, then we add a route to register, login and logout. You can see how the code run in this file routes\api.php

TRIALS

  • To test it, we first run our project first. Open terminal, then run the following command
      php artisan serve
    
  • Then you can open your postman, and try the endpoint below

AUTH ENDPOINT

REGISTER

Request :

  • Method : POST
  • Endpoint : /api/register
  • Header :
    • Accept: application/json
  • Body :
{
    "name": "string",
    "email": "string, email",
    "password": "string",
}

Response :

{
  "status": "boolean",
  "status_code": "integer",
  "message": "string",
  "data": {
      "id": "integer, unique",
      "name": "string",
      "email": "string",
      "created_at": "timestamp",
      "updated_at": "timestamp"
  },
  "access_token": "string, unique",
  "token_type": "string",
}

LOGIN

Request :

  • Method : POST
  • Endpoint : /api/login
  • Header :
    • Accept: application/json
  • Body :
{
    "email": "string, email",
    "password": "string",
}

Response :

{
  "status": "boolean",
  "status_code": "integer",
  "message": "string",
  "access_token": "string, unique",
  "token_type": "string",
}

LOGOUT

Request :

  • Method : POST
  • Endpoint : /api/logout
  • Header :
    • Accept: application/json
    • Autohorization: Bearer token

Response :

{
  "status": "boolean",
  "status_code": "integer",
  "message": "string",
  "access_token": "string, unique",
  "token_type": "string",
}

USER

Request :

  • Method : GET
  • Endpoint : /api/user
  • Header :
    • Accept: application/json
    • Autohorization: Bearer token

Response :

{
  "status": "boolean",
  "status_code": "integer",
  "message": "string",
  "data": {
      "id": "integer, unique",
      "name": "string",
      "email": "string",
      "created_at": "timestamp",
      "updated_at": "timestamp"
  },
  "access_token": "string, unique",
  "token_type": "string",
}

About

Learn how to create token based authentication for REST API using Laravel Sanctum. This project provides an API endpoint for registering, logging in, logging out, and getting data which is the implementation of one of Laravel Sanctum's uses, namely the API Token

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages