Download the PHP package fherryfherry/laravel-api-token without Composer
On this page you can find all versions of the php package fherryfherry/laravel-api-token. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fherryfherry/laravel-api-token
More information about fherryfherry/laravel-api-token
Files in fherryfherry/laravel-api-token
Package laravel-api-token
Short Description Make a tokenize for our API in Laravel is never been easy
License MIT
Informations about the package laravel-api-token
Laravel Simple API Token
This library is to Tokenize your current API Restful very easy. It makes your API more secure.
How this library works :
- It will create table
laravel_api_tokens
- Insert the token data into it
- Validate user request with table
laravel_api_tokens
Requirements
- Laravel 6, 7, or 8
- PHP 7.4 or 8.x
Installation
Run this command on your root project
Run the migration bellow
After installation is done, then run bellow command to export configuration file :
Configuration
Setting .ENV
Open the .env
file, and paste these bellow on the bottom of file
You could change its value.
Save User Data Into Token
You should create your own Login API. Then after the login is succeeded you could call this helper. For the first, add these bellow to top of the class
Then in your login method would be like these
Request Token Endpoint
Give this endpoint to your frontend engineer. (I assume you use artisan serve, instead adjust the base domain)
Add header parameter with Basic Authorization.
How to use Basic Authorization you could refer this document. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
This API will produce like these bellow :
Frontend engineer should save the expired_at
,access_token
,refresh_token
value.
Refresh Token Endpoint
This API is to extend the expired time of access_token
without request token again. But you will get new access_token
,refresh_token
,expired_at
.
The difference with Request Token is you don't need to hit the Login API again.
Frontend engineer need to add a Header Parameter with Bearer Authorization
This API will produce like these bellow :
Frontend engineer should save the expired_at
,access_token
,refresh_token
value. For next header authorization.
Secure Your API With Token
To prevent any user hit your API Without token, so you have to add laravel_api_token
middleware to your API Route.
Open your API route location (I assume you use routes/api.php)
Frontend engineer need to add a Header Parameter with Bearer Authorization
Get Current User ID
If you would like to get the current user ID, you only need to call this helper
Get Current User Name
If you would like to get the current user name, you only need to call this helper
Get Current User Role
If you would like to get the current user role, you only need to call this helper
Get Token Data
For whatever reason sometime you want to see all available column values of current token, you could call this helper
Destroy Token
If the user is logging out, you have to call this helper into your logout method at the bottom line is fine. So the frontend is should call the request token API again.
Support & Donation
Hi thanks for using my open source project, you could support me via : https://saweria.co/ferryariawan or via https://buymeacoffee.com/ferryariawan
Security Issue
If you found any security issue please contact me at ferdevelop15[at]gmail.com