Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Login Endpoint | Authentication with JWT
Professional Web API with Flask

bookLogin Endpoint

The class and method for login are similar to those for registration. Let's write it:

@blp.route("/login")
class UserLogin(MethodView):
    @blp.arguments(UserSchema)
    def post(self, user_data):
        user = UserModel.query.filter(
            UserModel.username == user_data["username"]
        ).first()

        if user and pbkdf2_sha256.verify(user_data["password"], user.password):
            access_token = create_access_token(identity=user.id)
            return {"access_token": access_token}, 200

        abort(401, message="Invalid credentials.")

We filter users in the database by username and check if such a user exists and if the password verification for this user passes. If so, we create a JWT access token.

In this method, we return an access token generated using the create_access_token function imported from the flask_jwt_extended library. We add the user ID information to the JWT access token so that when a user returns the JWT to us, we will know which user it belongs to.

If a user with the given username does not exist or the password does not match what is in the table, we return an error.

1. Which HTTP method is used by the UserLogin class to authenticate users?

2. What information is included in the JWT access token generated by the UserLogin method?

question mark

Which HTTP method is used by the UserLogin class to authenticate users?

Select the correct answer

question mark

What information is included in the JWT access token generated by the UserLogin method?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 3.03

bookLogin Endpoint

Glissez pour afficher le menu

The class and method for login are similar to those for registration. Let's write it:

@blp.route("/login")
class UserLogin(MethodView):
    @blp.arguments(UserSchema)
    def post(self, user_data):
        user = UserModel.query.filter(
            UserModel.username == user_data["username"]
        ).first()

        if user and pbkdf2_sha256.verify(user_data["password"], user.password):
            access_token = create_access_token(identity=user.id)
            return {"access_token": access_token}, 200

        abort(401, message="Invalid credentials.")

We filter users in the database by username and check if such a user exists and if the password verification for this user passes. If so, we create a JWT access token.

In this method, we return an access token generated using the create_access_token function imported from the flask_jwt_extended library. We add the user ID information to the JWT access token so that when a user returns the JWT to us, we will know which user it belongs to.

If a user with the given username does not exist or the password does not match what is in the table, we return an error.

1. Which HTTP method is used by the UserLogin class to authenticate users?

2. What information is included in the JWT access token generated by the UserLogin method?

question mark

Which HTTP method is used by the UserLogin class to authenticate users?

Select the correct answer

question mark

What information is included in the JWT access token generated by the UserLogin method?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 5. Chapitre 4
some-alt