Created
February 16, 2018 18:31
-
-
Save tkorakas/7926f3d8fcd04b892758532289a8e2dc to your computer and use it in GitHub Desktop.
Lumen Laravel JWT authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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