Adding isAuth and access_token to express-session in a Node.js web application is a straightforward

Initially, I set the request type to be "any", but upon realizing that it should not be "any" or "Request", I have decided to extend express-session in order to create a custom session type. How can I achieve this and incorporate the specified information into the request session?

access_token : string
userinfo: {
sub: 'Diur4_PcorTDFMRP99pgP9Qwkclxg4',
name: 'John Kay',
family_name: 'Kay',
given_name: 'John',
picture: 'https://graph.microsoft.com/v1.0/me/photo/$value',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cc86a3a4a2e287adb58caeada0adaeada0ade2afa3a1">[email protected]</a>'
}
isAuth:boolean

req.session.access_token = access_token;
req.session.userinfo = Userinformtion;
req.session.isAuth = true;  //save session

const isAuth = (req:any, res: Response, next: NextFunction) => {
if (req.session.isAuth) {
next();
} else {
res.render("landing");
}
};

Answer №1

If you want to include additional session data in an Express session, all you need to do is follow the same method outlined in your documentation. I have provided a functional example below:

export default (req: RequestI, res: Response): void => {
  const { timezone } = req.body;
  if (req.session) {
    if (timezone) {
      req.session.timezone = timezone;
    }
  }
  res.send();
};

The above code snippet represents a basic route handler for an express endpoint. By sending a POST request with the timezone parameter and making sure the user is logged in, their session will now store information about their timezone. You won't need any extra libraries aside from express and express-session for this functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improve the performance of the search results page by prioritizing the retrieval of the number

I am facing an issue with the loading time for displaying more than 1000 rows of results in my code. Is there a way to modify the code to first check the number of query results and then create pagination before fetching only the results for that specific ...

What is the best way to organize data by date in mongoose?

I have successfully populated the arrays with the necessary data, but I'm unsure of where to aggregate the mongoose data in order to group it by date. Below is an example of the mongo data I am working with: [{ "_id": "602badd5 ...

What is the best way to include TypeScript definition files in version control?

I am working on a basic asp.net core web application and have integrated javascript libraries using libman. Now, I am looking to incorporate typescript, therefore I have obtained typescript definition files for the libraries via npm. For example: npm ins ...

Showcase pictures within an angular smart table

Is it possible to display images in a column within an ng smart table? We have several columns consisting mostly of data, with one column dedicated to displaying images. Following the ng smart table concept, I attempted to implement the code below which cu ...

Showing dynamic icons in Angular 2 applications

My goal is to dynamically load a part of my website, specifically by using icon classes defined in the interface like this: import { OpaqueToken } from "@angular/core"; import {IAppConfig} from './app.interface' export let APP_CONFIG = new Opaq ...

Tips for sending an array to a jade partial once it has been fully loaded

I have a tightly organized array that I want to render seamlessly into a jade partial once it's fully assembled. (I've tested this by preloading it) The current challenge I'm facing, and can't seem to find a solution to anywhere, is th ...

What is the best way to determine the highest value?

How can I ensure that the data is displayed based on the condition c.date <= this.selectedReport.report_date? The current code snippet if (Math.max(...this.costs.map(c => c.date))){} seems to be causing an issue where no data is being displayed. What ...

Upgrade Vue by utilizing a class object attribute

I have created a unique class with properties and I am trying to figure out how to update my vue file to reflect any changes made to the properties. I have simplified the class for now, but plan to expand it with more properties in the future. In this spe ...

The element ''ag-grid-angular'' is unrecognized:

After using ag-grid successfully for a few months, I attempted to integrate a grid from a previous project into an Angular template like the one found in ngx-admin: ngx-admin This is how the template is structured: https://i.sstatic.net/Mfjw8.png I tr ...

Challenges faced when using an array of objects interface in Typescript

I have initialized an array named state in my component's componentDidMount lifecycle hook as shown below: state{ array:[{a:0,b:0},{a:1,b:1},{a:2,b:2}] } However, whenever I try to access it, I encounter the following error message: Prop ...

Is there a TypeScript type that represents a subset of the keys of another type?

When given an object, is it possible to create a second typed object that contains only a subset of the original keys with different value types? I attempted to use Partial<keyof ...>, but it did not have the desired effect. Is there another approach ...

Mean stack authentication issue: missing token

Overview: Currently, I'm in the process of developing an application that utilizes node/express for the backend, mongo as the database, and angular for the frontend. User authentication is handled through jsonwebtoken, where a token is stored in local ...

Issue: Unable to modify headers after they have been sent. This error occurred when reloading a GET request

Upon initial execution, the following code operates as expected. However, on subsequent runs, it fails with the error message: Error: Can't set headers after they are sent. I am attempting to retrieve a message from a series of published mqtt topi ...

What could be causing my dictionary to be empty when using res.send in conjunction with SQL Server and express.js?

I'm currently utilizing mssql in conjunction with express.js. My objective is to store the results of the recordset into a dictionary and then send that dictionary back using res.send, but I am encountering an issue where the dictionary appears to be ...

How to retrieve an object array from a node with the help of Axios

When the axios call is made and returns some data, I want to store that data in state. The useEffect function will then be triggered and populate the task list array. This populated array can be iterated over and displayed on the front end. import{ useEff ...

Creating a function in Typescript that transforms an array into a typed object

Recently, I have started learning TypeScript and I am working on a function to convert arrays from a web request response into objects. I have successfully written the function along with a passing unit test: import { parseDataToObject } from './Parse ...

Refine a union type by considering the properties already defined in an object

interface CustomHTMLElement { htmlPropA: string, htmlPropB: string, } interface CustomHTMLInput { inputPropA: string, inputPropB: string, } type CustomElement = | CustomHTMLElement | CustomHTMLInput const element: CustomElement = { inputPr ...

Creating composite type guards - passing down properties from the type guard to the calling function

type Bird = { fly: () => "fly" }; type Insect = { annoy: () => "annoy" }; type Dog = { beTheBest: () => "dogdogdog" }; type Animal = Bird | Insect | Dog; const isBird = (animal: Animal): animal is Bird => { if ...

An error has occurred in Node.js/Express.js: The path argument is missing and is required for res.sendFile

I have successfully uploaded my files using Multer to a specific directory. However, I am now facing an issue while trying to download the same files that are displayed in an HTML table with individual download options. Below is the code snippet: Node.j ...

Understanding and processing HTML strings in typescript

I am currently utilizing TypeScript. Within my code, there is an object named "Reason" where all variables are defined as strings: value, display, dataType, and label. Reason = { value: '<ul><li>list item 1</li><li&g ...