Skip to content

Instantly share code, notes, and snippets.

@tkorakas
Created February 16, 2018 18:31
Show Gist options
  • Save tkorakas/7926f3d8fcd04b892758532289a8e2dc to your computer and use it in GitHub Desktop.
Save tkorakas/7926f3d8fcd04b892758532289a8e2dc to your computer and use it in GitHub Desktop.
Lumen Laravel JWT authentication
<?php
namespace App\Providers;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Support\ServiceProvider;
class AuthServiceProvider extends ServiceProvider {
/**
* Register any application services.
*
* @return void
*/
public function register() {
//
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot() {
$this->app['auth']->viaRequest(
'api',
function ($request) {
if (!is_null($request->header('Authorization'))) {
$token = JWT::decode(
$request->header('Authorization'),
env('JWT_KEY'),
['HS256']
);
return User::where('email', '=', $token->email)->first();
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment