Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
User Registration | Authentication with JWT
course content

Contenido del Curso

Professional Web API with Flask

User RegistrationUser Registration

We have already defined a model and schema for the User. Additionally, we have several endpoints for reading user information.

Adding a User Registration Endpoint

In the resources/user.py file, we will add a new class UserRegister with a post method to allow users to register on our site. We specify the endpoint URL using the blueprint route decorator. For the post method, we use the blueprint arguments decorator that links the method to the UserSchema.

Username Existence Check

We write a condition to check for the existence of the same username in the database. If such a username already exists, we return an error. If the check passes and the username is unique, we create a new user with the data received in the user data dictionary.

Using Passlib for Password Hashing

We use the additional library passlib.hash from which we import pbkdf2_sha256 to ensure that plain passwords are not stored in the database. This way, even if our database is compromised, the attackers cannot easily read the passwords because they are encrypted when written to the database and cannot be decrypted back into a readable password.

We add the new user to the database and save the changes.

We also need to add a password field to our schemas, which will be mandatory and only for loading, not available for reading.

In the next chapters, we will write our next endpoints for login and logout.

1. What does the blueprint route decorator `@blp.route` specify in Flask?
2. What happens if a user tries to register with a username that already exists in the database?
3. If a registration attempt is made with a duplicate username, what HTTP status code should the error response ideally have?

What does the blueprint route decorator @blp.route specify in Flask?

Selecciona la respuesta correcta

What happens if a user tries to register with a username that already exists in the database?

Selecciona la respuesta correcta

If a registration attempt is made with a duplicate username, what HTTP status code should the error response ideally have?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 3
course content

Contenido del Curso

Professional Web API with Flask

User RegistrationUser Registration

We have already defined a model and schema for the User. Additionally, we have several endpoints for reading user information.

Adding a User Registration Endpoint

In the resources/user.py file, we will add a new class UserRegister with a post method to allow users to register on our site. We specify the endpoint URL using the blueprint route decorator. For the post method, we use the blueprint arguments decorator that links the method to the UserSchema.

Username Existence Check

We write a condition to check for the existence of the same username in the database. If such a username already exists, we return an error. If the check passes and the username is unique, we create a new user with the data received in the user data dictionary.

Using Passlib for Password Hashing

We use the additional library passlib.hash from which we import pbkdf2_sha256 to ensure that plain passwords are not stored in the database. This way, even if our database is compromised, the attackers cannot easily read the passwords because they are encrypted when written to the database and cannot be decrypted back into a readable password.

We add the new user to the database and save the changes.

We also need to add a password field to our schemas, which will be mandatory and only for loading, not available for reading.

In the next chapters, we will write our next endpoints for login and logout.

1. What does the blueprint route decorator `@blp.route` specify in Flask?
2. What happens if a user tries to register with a username that already exists in the database?
3. If a registration attempt is made with a duplicate username, what HTTP status code should the error response ideally have?

What does the blueprint route decorator @blp.route specify in Flask?

Selecciona la respuesta correcta

What happens if a user tries to register with a username that already exists in the database?

Selecciona la respuesta correcta

If a registration attempt is made with a duplicate username, what HTTP status code should the error response ideally have?

Selecciona la respuesta correcta

¿Todo estuvo claro?

Sección 5. Capítulo 3
some-alt