When using redis to store tokens, how can the previous token be cleared when the user logs in again?

  mongodb, question

I want to use it{ token: userId }The form of will be generated after logintokenSave inredisWhen the user usestokenAccording to the logintokenTo find the correspondingidHowever, when the user logs in again (such as changing the device and not exiting before), how should the previously saved ones be cleared?tokenOr reuse the ones that have not expired beforetoken.
The current solution is to set upredisExpiration time is cleared automatically, but I would like to ask other better solutions.

First of all, it must be automatically cleared after expiration.
Also, after the user logs in again, you should clear the token before the user. The key of your redis can be the id of the user plus the token.
For example, key:”skjfkskdlfjksdkfslkj#1 “can be used to find id through token or token through id.
When the user logs in again, you can definitely get the user’s id, then find the old data through the id, delete it at the time of addition or update.