Skip to content

This is a simple project demonstrating session and cookie management, as well as basic authentication using Express.js, EJS templates and MongoDB.

Notifications You must be signed in to change notification settings

MuhdHanish/session_cookies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Session and Cookie Management with Express.js

This is a simple project demonstrating session and cookie management, as well as basic authentication using Express.js, EJS templates, MongoDB for data storage, and bcrypt for password hashing.

Features

  • User Authentication: Users can sign up and log in securely.
  • Session Management: Sessions are maintained using express-session middleware and stored in MongoDB using connect-mongodb-session.
  • Password Hashing: Passwords are securely hashed using bcrypt before being stored in the database.
  • Request Validation: Request bodies are validated using express-validator middleware to ensure required fields are filled and email formats are valid.
  • Error Handling: Errors during signup, login, and logout are caught and appropriate error messages are rendered to the user.

Setup

  1. Install Dependencies:
    npm install
    
  2. Start the Server:
    npm run dev
  3. Access the Application:

Usage

Dependencies

  • Express.js: Web application framework for Node.js.
  • express-session: Middleware for session management.
  • express-validator: Middleware for request validation.
  • bcryptjs: Library for password hashing.
  • mongoose: MongoDB object modeling tool.
  • connect-mongodb-session: Session store for MongoDB.
  • EJS: Embedded JavaScript templates for server-side rendering.

What are Sessions and Cookies?

  • Sessions: Sessions are a way to store user data on the server side. When a user logs in, a session is created and a unique session ID is generated. This session ID is stored as a cookie in the user's browser. Subsequent requests from the same user include this session ID, allowing the server to identify the user and retrieve their session data.

  • Cookies: Cookies are small pieces of data stored in the user's browser. They are often used for tracking user sessions, storing user preferences, and implementing features like "remember me" functionality. In the context of session management, cookies are used to store session IDs, which are then used to retrieve session data from the server.

Authentication and Authorization

  • Authentication: Authentication is the process of verifying the identity of a user. In this project, users are authenticated using their email and password. Upon successful authentication, a session is created for the user, allowing them to access protected routes.

  • Authorization: Authorization is the process of determining whether a user has permission to access certain resources or perform certain actions. In this project, authorization is implemented using middleware functions to restrict access to certain routes based on whether the user is authenticated.

This README provides comprehensive information about the project, including setup instructions, usage guidelines, dependencies, and explanations of session and cookie management, as well as authentication and authorization.

About

This is a simple project demonstrating session and cookie management, as well as basic authentication using Express.js, EJS templates and MongoDB.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published