Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Logout Endpoint | Authentication with JWT
Professional Web API with Flask

bookLogout Endpoint

To enable a user to logout, we must revoke their JWT. If the user attempts to reuse the same JWT, they will be denied access to the system. To achieve this, we need to create a storage solution for keeping revoked JWTs until their expiration.

Creating a Blocklist for Revoked Tokens

First, we create a blocklist.py file and write:

BLOCKLIST = set()

Checking if Token is Revoked

Next, in app.py, we import our BLOCKLIST variable and define a new function to check if a token is in the blocklist:

from blocklist import BLOCKLIST
...
def create_app():
    ...
    jwt = JWTManager(app)
   ...
   @jwt.token_in_blocklist_loader
def check_if_token_in_blocklist(jwt_header, jwt_payload):
       return jwt_payload["jti"] in BLOCKLIST

Endpoint for User Logout

To facilitate user logout, we create a UserLogout class where we add the user's JWT to the BLOCKLIST:

@blp.route("/logout")
class UserLogout(MethodView):
   @jwt_required()
   def post(self):
       jti = get_jwt()["jti"]
       BLOCKLIST.add(jti)
       return {"message": "Successfully logged out"}, 200

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Awesome!

Completion rate improved to 3.03

bookLogout Endpoint

Свайпніть щоб показати меню

To enable a user to logout, we must revoke their JWT. If the user attempts to reuse the same JWT, they will be denied access to the system. To achieve this, we need to create a storage solution for keeping revoked JWTs until their expiration.

Creating a Blocklist for Revoked Tokens

First, we create a blocklist.py file and write:

BLOCKLIST = set()

Checking if Token is Revoked

Next, in app.py, we import our BLOCKLIST variable and define a new function to check if a token is in the blocklist:

from blocklist import BLOCKLIST
...
def create_app():
    ...
    jwt = JWTManager(app)
   ...
   @jwt.token_in_blocklist_loader
def check_if_token_in_blocklist(jwt_header, jwt_payload):
       return jwt_payload["jti"] in BLOCKLIST

Endpoint for User Logout

To facilitate user logout, we create a UserLogout class where we add the user's JWT to the BLOCKLIST:

@blp.route("/logout")
class UserLogout(MethodView):
   @jwt_required()
   def post(self):
       jti = get_jwt()["jti"]
       BLOCKLIST.add(jti)
       return {"message": "Successfully logged out"}, 200

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 7
some-alt