User Authentication System Setup Guide
User Authentication System Setup Guide
During user registration, the system collects a username and password, hashes the password using bcryptjs to ensure it is stored securely, and then inserts the hashed password and username into the SQL database. This process prevents storing plain text passwords, enhancing data security .
Bcryptjs is a suitable choice for password management as it provides a robust hashing mechanism designed to slow down potential brute-force attacks, thanks to its use of salting and iterative hashing. This mitigation against brute force and dictionary attacks makes it a strong option for securing passwords .
Session management is handled using JSON Web Tokens (JWT). Upon successful login, a JWT is generated and returned to the user. This token is stored client-side (e.g., in local storage) and must be included in the Authorization header for accessing protected routes, such as the dashboard. The system verifies the token on each request to ensure its validity, ensuring only authenticated users gain access .
Middleware in Express.js serves as functions that process requests between the server and the client. They are used here to parse JSON payloads in requests using body-parser, handle authentication processes, manage errors, and handle custom routing logic effectively, ensuring code modularity and reusability .
The system establishes a connection to the MySQL database using mysql.createConnection. Upon connection initiation, it checks for errors, throwing an exception if any occur. This step ensures feedback is provided immediately if the database connection fails, allowing for timely debugging and resolution .
Client-side JavaScript enhances user experience by managing dynamic interactions, such as collecting user credentials for registration and login, displaying feedback through interface alerts, and storing session tokens in local storage post-authentication. It facilitates asynchronous requests via fetch API, enhancing responsiveness and interaction flow .
The login process involves verifying the user-provided username and password against the stored credentials in the database. The system uses bcryptjs to compare the password input with the stored hashed password. Upon successful verification, a JSON Web Token (JWT) is generated and returned to the client, which can be used for authorized access to protected routes .
SQL is used for persistent storage of user credentials, ensuring they are organized efficiently for retrieval and modification. The database schema supports user management by providing functions such as INSERT for new user registration and SELECT for retrieving user data during login attempts. It also manages primary keys to uniquely identify user records .
The user authentication system utilizes Node.js as the backend runtime environment for running server-side code. Express.js is used as a web framework to manage routes and middleware operations. SQL is employed to store and manage user data securely, and JavaScript is used for client-side interactions and validations .
Storing JWTs in local storage exposes the application to potential attacks like Cross-Site Scripting (XSS), where malicious scripts could access the tokens. To mitigate this, developers could opt for HTTP-only cookies, which are not accessible via client-side scripts, or implement advanced measures like Content Security Policy (CSP) to reduce the likelihood of XSS attacks .