PHP code example of sunding0308 / laravel-api-auth

1. Go to this page and download the library: Download sunding0308/laravel-api-auth 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/ */

    

sunding0308 / laravel-api-auth example snippets


    SunD1ng\LaravelApiAuth\ServiceProvider::class,
    

    php artisan vendor:publish --provider="SunD1ng\LaravelApiAuth\ServiceProvider"
    

    protected $routeMiddleware = [
        'api_auth' => \SunD1ng\LaravelApiAuth\Middleware::class,
        // other ...
    ];
    

    php artisan api_auth
    

    'roles' => [
        '{access_key}' => [
            'name' => '{role_name}',        // 角色名字,例如 android
            'secret_key' => '{secret_key}',
        ],
    ],
    

   
    /**
     * User: sunding0308
     * Date: 2018/4/16
     * Time: 下午3:22
     */
    
    namespace SunD1ng\LaravelApiAuth\Signatures;
    
    
    class Md5 implements SignatureInterface
    {
        public static function sign(string $string, string $secret): string
        {
            return md5($string . $secret);
        }
    
        public static function check(string $string, string $secret, string $signature): bool
        {
            return static::sign($string, $secret) === $signature;
        }
    
    }
    

Route::get('api/example', function(Request $request){
    // $request->get('client_role');
    // todo...
})->middleware(['api_auth']);

\\ or

Route::group(['middleware'=>'api_auth'], function(){
    // routes...
});