Last active
February 7, 2018 04:00
-
-
Save salipro4ever/8e1ef3ab4bdbc6956196d19386e7d11a to your computer and use it in GitHub Desktop.
JWT auth for Lumen 5.5 & tymon/jwt-auth
This file contains hidden or 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
| composer.json | |
| ... | |
| "require": { | |
| "php": ">=7.0", | |
| "laravel/lumen-framework": "5.5.*", | |
| "vlucas/phpdotenv": "~2.2", | |
| "tymon/jwt-auth": "1.0.0-rc.1" | |
| }, | |
| ----------- | |
| $ composer update | |
| ---------- | |
| Uncomment | |
| $app->routeMiddleware([ | |
| 'auth' => App\Http\Middleware\Authenticate::class, | |
| ]); | |
| Add | |
| $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class); | |
| -------- | |
| REF: https://github.com/tymondesigns/jwt-auth/tree/develop/docs |
This file contains hidden or 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; | |
| use Illuminate\Auth\Authenticatable; | |
| use Laravel\Lumen\Auth\Authorizable; | |
| use Illuminate\Database\Eloquent\Model; | |
| use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; | |
| use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; | |
| use Tymon\JWTAuth\Contracts\JWTSubject; | |
| class User extends Model implements AuthenticatableContract, AuthorizableContract, JWTSubject | |
| { | |
| use Authenticatable, Authorizable; | |
| /** | |
| * The attributes that are mass assignable. | |
| * | |
| * @var array | |
| */ | |
| protected $fillable = [ | |
| 'name', 'email', | |
| ]; | |
| /** | |
| * The attributes excluded from the model's JSON form. | |
| * | |
| * @var array | |
| */ | |
| protected $hidden = [ | |
| 'password', | |
| ]; | |
| public function getJWTIdentifier() | |
| { | |
| # !important cmnr | |
| return $this->getKey(); | |
| } | |
| public function getJWTCustomClaims() | |
| { | |
| return []; | |
| } | |
| } |
This file contains hidden or 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
| return [ | |
| 'defaults' => [ | |
| 'guard' => env('AUTH_GUARD', 'api'), | |
| ], | |
| 'guards' => [ | |
| 'api' => ['driver' => 'jwt','provider' => 'users'], | |
| ], | |
| 'providers' => [ | |
| 'users' => [ | |
| 'driver' => 'eloquent', | |
| 'model' => App\User::class, | |
| ], | |
| ], | |
| 'passwords' => [ | |
| // | |
| ], | |
| ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment