Introducci贸n a Node.js y Express
Node.js y Express
Entorno de trabajo
Variables de entorno
Rutas din谩micas
Body parser
Postman
CRUD
驴Que es una API Restful?
Solicitudes GET
Solicitudes POST
CRUD
Soluci贸n del reto de validaci贸n
Solicitudes DELETE
MIddlewares
Middlewares - logger
Middlewares - ErrorHandler
DB
Instalar Postgresql
Instalar Prisma y PostgreSQL Client
Prisma Models
JWT
Autenticaci贸n utilizando JSON Web Tokens (JWT)
Auth - Register
Auth - Login
Expres.js
Arquitectura
Arquitectura parte 2
Creaci贸n y Migraci贸n de Modelos con Prisma para Citas M茅dicas
Admin controllers / services
Admin controllers / services parte 2
Reservations
Reservations parte 2
Appointments
Deploy
PostgreSQL
Deploy
You don't have access to this class
Keep learning! Join and start boosting your career
User authentication is a fundamental component of modern web application development. Implementing a secure registration system not only protects your users' information, but also lays the foundation for more advanced features such as protected paths and custom access levels. In this content, we will explore how to create an entry point for registering users in a database using Node.js, Prisma ORM and encryption techniques.
When developing web applications, the security of our users' data must be a priority. Implementing a robust registration system involves not only storing user information, but also protecting sensitive data such as passwords using encryption techniques.
To create our log entry point, we need to:
First, we need to set up our POST route to receive the new user's data:
app.post('/register', async (req, res) => { // Extract information from the request body const { email, password, name } = req.body;
// Encrypt the password const hashedPassword = await bcrypt.hash(password, 10);
// Create user in database const newUser = await prisma.user.create({ data: { email, password: hashedPassword, name, role: 'user' } });
// Send response res.status(201).json({ message: 'User registered successfully' });});
This code sets up a /register
entry point that accepts POST requests with the user's data. The password is encrypted using bcrypt with a cost factor of 10, which provides a good balance between security and performance.
Password security is crucial in any authentication system. We should never store passwords in plain text in our database. Instead, we use hash algorithms such as bcrypt:
const bcrypt = require('bcrypt');constjwt = require('jsonwebtoken');
We must import these libraries at the beginning of our file in order to use them. bcrypt allows us to generate a secure hash of the password, while JWT (JSON Web Token) will be used later for authentication.
The cost factor (in this case 10) determines how much computational work is required to generate the hash. A higher value provides better security but consumes more resources.
Prisma makes it easy for us to interact with the database using an ORM (Object-Relational Mapping). To create a new user, we use the create
method of the User
model:
const newUser = await pr isma.user.create({ data: { email, password: hashedPassword, name, role: 'user' } });
This code creates a new record in the user table with the provided data. The ID is automatically generated thanks to the model configuration in Prisma.
Once the code is implemented, it is important to verify that it works correctly. We can use tools like Postman to send requests to our endpoint and test the responses.
To test our registration endpoint:
http://localhost:3000/register)
{ " email": "[email protected]", " password": "password123", " name": "Example User"}
If everything works correctly, we should receive a 201 (Created) status code and a message indicating that the user has successfully registered.
To confirm that the data has been stored correctly, we can create a temporary endpoint that allows us to list the users:
app.get('/db/users', async (req, res) => { const users = await prisma.user.findMany(); res.json(users);});
This endpoint should be removed in production as it exposes sensitive information, but it is useful during development to verify that:
Implementing a secure registration system involves more than just encrypting passwords. Some important considerations:
/db/users
must be eliminated in production.Security is an ongoing process, not a feature that is implemented once and forgotten. We must be constantly updating and improving our security practices.
Implementing a secure login system is only the first step in creating a complete authentication system. In future steps, we will develop user login and validation of protected paths using JWT tokens.
Have you implemented authentication systems in your projects? What security techniques do you use to protect your users' data? Share your experience in the comments.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?