ci-secure-api icon indicating copy to clipboard operation
ci-secure-api copied to clipboard

JWT issue

Open vesvello opened this issue 2 years ago • 1 comments

I need to replace jwt_helper.php with this:

use App\Models\UserModel; use Config\Services; use Firebase\JWT\JWT; use Firebase\JWT\Key;

function getJWTFromRequest($authenticationHeader): string { if (is_null($authenticationHeader)) { //JWT is absent throw new Exception('Missing or invalid JWT in request'); } //JWT is sent from client in the format Bearer XXXXXXXXX return explode(' ', $authenticationHeader)[1]; }

function validateJWTFromRequest(string $encodedToken) { $key = Services::getSecretKey(); $decodedToken = JWT::decode($encodedToken, new Key($key, 'HS256'));
$userModel = new UserModel(); $userModel->findUserByEmailAddress($decodedToken->email); }

function getSignedJWTForUser(string $email) { $issuedAtTime = time(); $tokenTimeToLive = getenv('JWT_TIME_TO_LIVE'); $tokenExpiration = $issuedAtTime + $tokenTimeToLive; $payload = [ 'email' => $email, 'iat' => $issuedAtTime, 'exp' => $tokenExpiration, ];

$jwt = JWT::encode($payload, Services::getSecretKey(), 'HS256'); return $jwt; }

vesvello avatar Nov 27 '22 02:11 vesvello

Thanks, this was a helpful post - I ran into the same issue

dlm423 avatar Mar 18 '24 21:03 dlm423