PHP code example of fherryfherry / laravel-api-token

1. Go to this page and download the library: Download fherryfherry/laravel-api-token library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fherryfherry / laravel-api-token example snippets

 


return [

    "expiry_unit"=> "day", // day, hour, minute
    "expiry_duration" => 3, // expiry duration by unit

    "token_length"=> 128, // how long token is

    // VALIDATION LEVEL ============================================ //
    // Level 1 = Validate by token only (default)                    //
    // Level 2 = Validate by token and ip address                    //
    // Level 3 = Validate by token, ip address and user agent        //
    //                                                               //
    // Please be careful with validation 2 and 3 because ip address  //
    // can suddenly change. Usually this because user providers      //
    // ============================================================= //
    "validation_level"=> 1,

    "basic_auth_user" => env("BASIC_AUTH_USER"), // user to request token
    "basic_auth_pass" => env("BASIC_AUTH_PASS") // password to request token
];
 
use FherryFherry\LaravelApiToken\Helper\LaravelSimpleApiToken;
 
public function postLogin(Request $request) {
    // ...
    
    if(Auth::attempt($request->except("_token"))) {
        // Then after that call this helper
        LaravelSimpleApiToken::saveLoginData($request, $user->id, $user->name);
        
        // Or if you have a role
        LaravelSimpleApiToken::saveLoginData($request, $user->id, $user->name, $user->role);               
    }
       
    // ...
}
 
Route::middleware(['api','laravel_api_token'])->group(function() {
    // place your all api routes here
    // ...
    
});
 
$currentUserID = LaravelSimpleApiToken::getUserId();
 
$currentUserName = LaravelSimpleApiToken::getUserName();
 
$currentUserRole = LaravelSimpleApiToken::getUserRole();
 
$tokenData = LaravelSimpleApiToken::getTokenData();
 
LaravelSimpleApiToken::destroy($request);
bash 
php artisan migrate
bash 
php artisan vendor:publish --provider=FherryFherry\LaravelApiToken\LaravelSimpleApiTokenServiceProvider