This is the sample Laravel API apps with integrating Sanctum Authentication for User
- 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
- Laravel 8.83.26
- PHP 7.4.19
- MySQL 14.14
- Run
cp .env.example .env
command to copy example into real.env
file, then edit it with DB credentials and other settings you preferred. - Run
composer install
command. - Run
php artisan key:generate
command.
- Run
php artisan serve
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
- [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
- [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
.
Notes: This is the TDD to test functionality of the API (ItemModel)
To run the TDD in Laravel:
php artisan test
Created by Lukman Imran Feel free to contact me: LinkedIn
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
The Laravel framework is open-sourced software licensed under the MIT license.