Gin framework writes web app, to authenticate users with jwt, how to log out and log in?

gin framework writes web app, to authenticate users with jwt, how to log out? The
login logic is to compare the username and password, generate jwt, if correct, and then save the client to localstorage.

question:
what should be the logic for logging in ?

Jan.28,2022

add a parameter to jwt. If you log out, change this parameter, so that jwt verification will not pass. This can be used as a condition for any invalidation. For example, if you change the password, you need to log in again. Just change this parameter


misunderstood the meaning of the subject and edit it again! Token can't quit voluntarily, just like session. You can add a blacklist on the server and declare a slice type called slice_token,. For example, if user 1 clicks to exit, its token1 is stored in slice_token,. The verification of each request is not taken from the list of token. Enter the slice loop to traverse before. If you indicate that you have exited, you will return the effect that the authorization failed, that is, you have logged out. But to slice_token this slice to do some data addition and deletion processing, it is impossible to store more and more all the time. Compare the token1 in the stored slice_token to the list of the real token, and if it fails, it will be deleted. This is simple and rude, but additions and deletions are performed every time a request is made. It makes more sense to introduce a cache such as redis, to store the blacklist list and verify the blacklist list in the cache each time. Delete the invalid blacklist (here is the token1 that the user clicks to exit). Write another timer to operate the blacklist in the cache. It has nothing to do with the main business and can be uncoupled

.
Menu