Invalidating JSON Web Tokens

前端 未结 28 2979
夕颜
夕颜 2020-11-22 06:17

For a new node.js project I\'m working on, I\'m thinking about switching over from a cookie based session approach (by this, I mean, storing an id to a key-value store conta

28条回答
  •  时光说笑
    2020-11-22 06:48

    You can have a "last_key_used" field on your DB on your user's document/record.

    When the user logs in with user and pass, generate a new random string, store it in the last_key_used field, and add it to the payload when signing the token.

    When the user logs in using the token, check the last_key_used in the DB to match the one in the token.

    Then, when user does a logout for instance, or if you want to invalidate the token, simply change that "last_key_used" field to another random value and any subsequent checks will fail, hence forcing the user to log in with user and pass again.

提交回复
热议问题