Skip to content

Commit

Permalink
Merge pull request #2 from tzkmx/evaluate-user-policy
Browse files Browse the repository at this point in the history
llamada a helper de usuario para validar user
  • Loading branch information
tzkmx authored May 28, 2021
2 parents 7af986d + c436fff commit 8b4c0c2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"require": {
"php": "^7.4",
"laravel/framework": "^6.0 | ^7.0 | ^8.0",
"rbdwllr/reallysimplejwt": "^2.1"
"laravel/framework": "^6.0 | ^7.24.0 | ^8.0",
"rbdwllr/reallysimplejwt": "^2.0"
},
"require-dev": {
"orchestra/testbench": "^4.0",
Expand Down
8 changes: 8 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Authenticatable;
/*
* Configurable:
* - cookie and request input key to use as fallback is token
Expand Down Expand Up @@ -50,4 +52,10 @@
'guard' => 'jwt',
'driver' => 'jwt',
'provider' => 'jwt',
'policy' => function (Request $request, Authenticatable $user = null) {
if(
method_exists($user, 'hasPendingConfirmation') &&
$user->hasPendingConfirmation()
) return null;
},
];
28 changes: 26 additions & 2 deletions src/JwtGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ public function user()
$token = $this->getPosibleToken($this->request);

$userId = $this->tokenService->getAuthIdentifier($token);

// TODO: verificar contra Exception caso de token no válido contra user not found
if (blank($userId)) return null;

$this->user = $this->provider->retrieveById($userId);
$this->user = $this->evaluatePolicy(
$this->provider->retrieveById($userId)
);

return $this->user;
}
Expand Down Expand Up @@ -87,9 +92,28 @@ protected function getPosibleToken(Request $request)

return '';
}


protected function evaluatePolicy($user = null): ?Authenticatable
{
return call_user_func_array(
config('jwt-auth.policy'),
[$this->request, $user]
);
}

public function viaRemember()
{
logger(__METHOD__);
return false;
}

public function logout()
{
logger(__METHOD__);
}

public function logoutCurrentDevice()
{
logger(__METHOD__);
}
}

0 comments on commit 8b4c0c2

Please sign in to comment.