RSS DEV Community

Minha implementação de Autenticação com JWT e Bcrypt

The author implemented stateless authentication using Node.js, Express, PostgreSQL, bcryptjs for password hashing, and JSON Web Tokens (JWT) for session management in their personal project. The project structure is divided into three main parts: authRoutes, authController, and utils/auth. The authController contains the business logic, including validating data, interacting with the database, and deciding on responses. The utils/auth file contains complementary functions for password and token generation to avoid code repetition. To register a user, the password is hashed using bcryptjs to create an irreversible hash. The authController handles this process in the `registerUser` function. After validating the password during login, a JWT token is generated using a secret key stored in environment variables. A middleware function is implemented to protect routes by verifying the JWT token, and access is denied if the token is invalid or missing. The author asks for feedback on their implementation and if others would do it differently.
dev.to
dev.to
Create attached notes ...