w24 MADS 4012 Session 9 THU FEB 8 Writing Middleware

Writing middleware for use in Express apps

Learning Outcomes:
Writing middleware in Express.js:
Access to Request and Response Objects:
Middleware functions have access to the request object (req) and the response object (res), allowing them to modify the request and response as needed ​Next Function:
Middleware functions have access to the next function in the application’s request-response cycle, which, when invoked, executes the next middleware in the stack. This allows for the sequential execution of middleware functions ​Error Handling:
Middleware functions can be used for error handling. If a middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging ​Promise Support:
Starting with Express 5, middleware functions that return a Promise will call next(value) when they reject or throw an error. This allows for better error handling in asynchronous operations
Flexibility and Extensibility:
Express.js provides a variety of built-in and third-party middleware libraries to achieve basic functionalities, allowing developers to create composable web applications by using and creating custom middleware functions
These highlights showcase the power and flexibility of middleware in Express.js, enabling developers to effectively manage the request-response cycle and create robust web applications.

Session 9 Learning Objectives: Authentication and Authorization

- Authentication and Authorization
- What is the difference between authentication and authorization? - **Definition of Authentication**: Process of verifying who a user is.
- **Definition of Authorization**: Process of verifying what a user has access to.
Authentication is about verifying identity, while authorization is about granting access based on that identity.
- What is session based authorization
- How to protect routes using the express-sessions library
- How to setup authentication with MongoAtlas + Mongoose
- Create a user account
- Login to a user account

Learning Guide: Authentication and Authorization

This learning guide is designed to provide a comprehensive understanding of authentication and authorization, their differences, session-based authorization, and practical implementation using Express.js and MongoDB Atlas with Mongoose.
Introduction to Authentication and Authorization
- **Authentication vs. Authorization**:
Authentication is the process of verifying who a user is, while authorization is the process of verifying what they have access to.
**Importance in Security**: Discuss how both processes are crucial in protecting applications and data from unauthorized access.

Understanding Session-Based Authorization

Definition and Workflow:
Session-based authentication as a stateful technique where sessions keep track of authenticated users.

HTTP and Statelessness:

HTTP is stateless - sessions provide a way to maintain state across requests.

Protecting Routes with express-session

Introduction to express-session
Introduce the express-session middleware and its role in managing session data.
Setting Up express-session
Discuss how to configure express-session in an Express.js application, including setting cookie options for security.

Authentication with MongoAtlas and Mongoose

- **MongoDB Atlas Setup**: Explain how to set up a MongoDB Atlas cluster and connect it using Mongoose[4].

User Model Creation:
Creating a user model with Mongoose to store user information.

Lab Activities

Activity 1: Implementing Session-Based Authentication (45 Minutes)

- **Objective**: Implement session-based authentication in a simple Express.js application.

- **Tasks**: 1. Set up an Express.js application. 2. Integrate express-session to manage user sessions. 3. Create login and logout routes to authenticate users.
#### Activity 2: Route Protection (30 Minutes)
- **Objective**: Protect specific routes in an Express.js application using session-based authentication. - **Tasks**: 1. Define a middleware function to check for authenticated sessions. 2. Apply the middleware to routes that require authentication.
#### Activity 3: Authentication with MongoDB Atlas (45 Minutes)
- **Objective**: Implement a user authentication system using MongoDB Atlas and Mongoose. - **Tasks**: 1. Connect to MongoDB Atlas using Mongoose[4]. 2. Create a user schema and model with Mongoose. 3. Implement routes for user registration and login, including password hashing and session management.

Lab Activities
Continue to experiment with different configurations and explore additional middleware for security, such as Helmet for setting various HTTP headers.
Highlight the importance of keeping dependencies up to date and following best practices for security in web applications.
Post-Lab Learning Outcomes:
Solid foundation in authentication and authorization, enabling programmers to implement secure web applications using Express.js and MongoDB.
Citations: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25]
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.