A
A
Alexey2017-10-18 22:21:25
PHP
Alexey, 2017-10-18 22:21:25

How to reset the token when the user logs out?

Hello! I'm trying to implement authorization for an API using JWT ( JWT library ).
The following logic comes out:
The user enters a login and password on the client side (android application).
They arrive at the server and if the login and password match, this code is executed:

$token = (new Lcobucci\JWT\Builder())	
                        ->setExpiration(time() + 3600000) // Время жизни токена
                        ->set('user_id', 1453535345) // в токене храним ID юзера
      ->sign($signer, 'testing') // ключ подписи
                        ->getToken();
echo (string) $token; // отправляем токен пользователю

The application sends this token with every request to the server, and at the very beginning of any code, the token is checked like this:
$token = (new Lcobucci\JWT\Parser())->parse((string) $_GET['token']); // парсим полученый токен
$data = new Lcobucci\JWT\ValidationData();

if($token->validate($data) && $token->verify($signer, 'testing')){
$_SESSION['user_id'] = $token->getClaim('user_id');
}

And then we are sure that this user_id is correct, since the signature of the token has been verified. So we will use everywhere to obtain data relating to this user. But what if the user decides to leave the profile? After all, we do not store data in the database about the relevance of the token, and after clicking on the exit button , what needs to be "killed" ? After all, the token will remain with the user, and also when sending it to the server, the same user_id will be determined. $_SESSION['user_id']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2017-10-18
@Griboks

Perhaps generating a new token will kill the old one. Unless it's the same one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question