You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
11 Hrs
10 Min
21 Seg

Crear Usuario

23/38
Resources

How to securely store a user in the database?

When developing applications, proper and secure handling of user data is essential. In this context, we will focus on how to securely store a user in the database after preliminary validation.

How to validate and extract data from the parameter object?

At this stage, we have validated that both the email and the password are valid data. Now, we need to store this data in the database and handle it securely. We start by changing the way we extract the parameters:

const { email, password } = await Joi.validate(req.body, schema);

This code block uses destructuring to assign the email and password values directly to specific variables, thanks to the previous validation with Joi. This simplifies access to these parameters in subsequent functions.

How do we store data using models?

Once the data has been obtained and validated, it is stored in the database. The next step is to use models to interact with it:

const user = await Users.create({ email, password });

This fragment shows the creation of a user in the database, using the create method of the Users model. Thanks to the asynchrony and the use of await, the write operation to the database is handled efficiently, without the need to import files due to the global way in which the models are defined.

Why is it important to encrypt passwords?

When storing data in the database, security is a priority. A common and critical mistake is to store passwords in plain text, something that could leave the application vulnerable to attack. If a hacker gains access to this unencrypted data, they could impersonate users. In addition, if the application handles sensitive data such as credit card information, the risk increases considerably.

The next essential step is to encrypt passwords before storing them, thus preventing their exposure. This process protects not only users but also the overall integrity of the application.

What is the next step in password security?

In the next stage, password encryption will be explored to ensure that even if unauthorized access is gained to the database, the passwords are unreadable. For encryption, specialized algorithms are used that convert passwords into unrecognizable strings of text that cannot be easily reversed.

Securing data does not end with validation and initial saving. Implementing robust security practices, such as encryption, is vital to protecting user information and maintaining the trustworthiness of your application. We encourage developers to continue to learn and implement data protection techniques that meet today's needs and regulations.

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Mi modelo usuario:

module.exports = {

  attributes: {

    name: { type: 'string' },
    username: { type: 'string' },
    email: { type: 'string' },
    password: { type: 'string' },

    // El rol de usuario dentro del sistema
    role: {type: 'string', isIn: ['superadmin', 'admin', 'guest'], defaultsTo: 'guest' },

    // Plan del usuario: basic es un usuario gratuito, expert es usuario premium.
    plan: { type: 'string', isIn: ['basic', 'expert'], defaultsTo: 'basic' },

    // Si el usuario valido via correo electronico
    activated: { type: 'boolean', defaultsTo: false },

    // Si es un usuario verificado, como Twitter
    verified: { type: 'boolean', defaultsTo: false },

    // Si el usuario esta activo dentro de la plataforma
    status: { type: 'string', isIn: ['active', 'deleted'], defaultsTo: 'active' },

    // Perfil public o privado
    isPrivate: { type: 'boolean', defaultsTo: true }

  },
 
};