Skip to content

Simple Laravel API Apps with Sanctum Authentication

Notifications You must be signed in to change notification settings

arlharis/laravel-test-api

Repository files navigation

Build Status Total Downloads Latest Stable Version License

Laravel-Test-API

This is the sample Laravel API apps with integrating Sanctum Authentication for User

Table of Contents

General Information

  • This apps is created to show how is Laravel API with Sanctum Authentication works
  • The API should contain:
    • An endpoint that takes a key (string) and value (JSON string) and store them
    • An endpoint that takes a key and returns the corresponding latest value of that key
    • An endpoint that takes a key and a timestamp and returns whatever the value of the key was at the given timestamp
    • An endpoint that will display all the key values pairs
  • Currently only have 2 current Models to test:
    • User
    • Item
  • Have two separated routes to show how sanctum authentication works
    • Public Route
    • Private Route

Technologies Used

  • Laravel 8.83.26
  • PHP 7.4.19
  • MySQL 14.14

Setup

Installation

  1. Run cp .env.example .env command to copy example into real .env file, then edit it with DB credentials and other settings you preferred.
  2. Run composer install command.
  3. Run php artisan key:generate command.

Running the server locally

  1. Run php artisan serve

Usage

This application does not have UI as it is developed to demo about API. You need to use any API test platform, for my case I am using Postman

Please initialize the headers like below to get the JSON format of output:

Key Value
Accept application/json

To store the data using API, you need to add this following key and value:

Key Value
name* name_value
description description_value

Notes: Please use the slug to update/delete and retrive single data

To Access Public Routes

  • [POST] - localhost:8000/api/register - Register User (To get the token)
  • [POST] - localhost:8000/api/login - Login to existing User (Get the token)
  • [GET] - localhost:8000/api/item/all - Retrieve all Items
  • [GET] - localhost:8000/api/item/{item} - Show single Item
  • [GET] - localhost:8000/api/item/search/{keyword} - Search method

To Access Private Routes

  • [POST] - localhost:8000/api/item - Create new Item
  • [PUT] - localhost:8000/api/item/{item} - Update existing Item
  • [DELETE] - localhost:8000/api/item/{item} - Delete existing Item
  • [POST] - localhost:8000/api/logout - Logout (Terminate session)

Notes: To access the private routes, you need to get the token (copy the token after you successfully login or registered)

  • Go to Authorization section of the Postman.
  • Select Bearer Token.
  • Paste the token.

Test-Driven Development (TDD)

Notes: This is the TDD to test functionality of the API (ItemModel)

To run the TDD in Laravel:

php artisan test

Acknowledgement

Contact

Created by Lukman Imran Feel free to contact me: LinkedIn

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

License

The Laravel framework is open-sourced software licensed under the MIT license.

About

Simple Laravel API Apps with Sanctum Authentication

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages