req.user is limited to containing only the username field

I recently started using passport js and encountered an issue. When I console.log req.user, it only displays the username field as {username: 'a'}. Other fields like email are not shown in the output.

Below is the content of index.js file:

const express = require("express");
const app = express();
// other required modules
...

The code for passport-config.js file is as follows:

const User = require("./models/User");
const bcrypt = require("bcryptjs");
// other passport configurations
...

And here is the user model:

const mongoose = require("mongoose");
const userSchema = new mongoose.Schema(
    {
        // user schema definition
    },
    {
        timestamps: true,
    }
);
const User = mongoose.model("User", userSchema);
module.exports = User;

If anyone can help me identify the issue in my code, I would greatly appreciate it.

Answer №1

To include additional properties in the req.user object, ensure to define them within the deserializeUser function:

passport.deserializeUser((id, cb) => {
  User.findOne({ _id: id }, (err, user) => {
    
    const userDetails = {
      username: user.username,
      email: user.email,
      // Include other fields as needed...
    };
    cb(err, userDetails);
  });
});

A detailed explanation of how the serializeUser and deserializeUser functions operate can be found in this insightful response.

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

Upon being provided with a route param, the response will be

For my current project using Express, I want to implement Reddit-like functionality where appending ".json" to any URL will return JSON data instead of the rendered template. To set up the rendering engine in Express, I am using Jade. Here is how I config ...

Is it possible to modify the default location where `live-server` opens without adjusting my primary web browser?

I am looking to modify the default browser that live-server opens in. Currently, it always opens in Safari, but I prefer using Google Chrome. I know there is a command live-server --browser='google chrome' to open it in Chrome, but I don't w ...

Struggling to link AWS S3 ReactApp with Heroku Node/Express API

I successfully deployed a react-app to AWS S3 and a node/express API on Heroku, but I am facing difficulties in connecting them together even with the cors configuration in the API or using proxy in the react-app. I have been unable to resolve this issue. ...

I need guidance on how to successfully upload an image to Firebase storage with the Firebase Admin SDK

While working with Next.js, I encountered an issue when trying to upload an image to Firebase storage. Despite my efforts, I encountered just one error along the way. Initialization of Firebase Admin SDK // firebase.js import * as admin from "firebas ...

What is the simplest method for transferring data to and from a JavaScript server?

I am looking for the most efficient way to exchange small amounts of data (specifically just 1 variable) between an HTML client page and a JavaScript server. Can you suggest a script that I can integrate into my client to facilitate sending and receiving d ...

Tips on efficiently compressing JSON data in order to receive it using the bodyParser.json method

I am looking to compress a JSON file before sending it to my server. I want to handle the compression in the browser by utilizing an explainer and then pass it to the bodyParser.json middleware. The client-side function would look something like this: e ...

Attempting to fetch package from an unconventional repository via npm install

When I work on my Node project at the office on a Mac, everything runs smoothly. However, when I try to work on it from home on a Windows machine, I encounter an access rights error while attempting to run npm install. Within my package.json, I have liste ...

Utilizing Folders in Views on Your Express Application

Within my views folder, I have a pug template named fixed-assets.pug located in views/administration/assets/ The main template default.pug, which the fixed-assets.pug extends from, is situated in the root views directory. Upon attempting to render the fi ...

The command `node server.js` has been initiated. Nodemon has executed a clean exit and is now waiting for any

A React application has been developed where users can input their email and a message to send to the server. However, upon trying to send the message, Error number 2 is encountered along with viewing Error number 1 in the terminal. Despite ensuring that ...

Showcase the data stored in express-session in a React application

I set up an OpenID passport.js strategy (passport-steam) for my MERN-stack application. The currently logged-in user's data is stored in an express-session, accessible through the object req.session.passport.user in my Node.js file. What is the most ...

Customizing the `toString()` method in Node.js exports

I'm having trouble overriding a toString() method in my code. I've already checked here and here, but haven't been able to solve the issue. This is what my code looks like: var Foo = function(arg) { // some code here... return fun ...

When I incorporate Express into my React project, I encounter an error stating that the prototype

After attempting to set up a basic React project that connects to a MySQL database, I encountered an error. When requiring 'express' and rebuilding the project, I received the following message when trying to open it in the browser: "Uncaught Ty ...

What benefits does NPM offer compared to using a script include?

I'm currently exploring the ins and outs of NPM. Can you shed some light on the benefits of using NPM instead of a script include? ...

Tips on showing a response in an HTML node with Node.js and AngularJS

Just starting out with NodeJS and AngularJS. My goal is to fetch a response from an external site and display it in Angular JS. I managed to send the request and receive a response, but on the UI JSON appears as a string with forward slashes "\". For ...

What is the process for compiled node projects to manage modifications to internal files?

I am currently developing a small program using nodejs that I intend to integrate as a backend service for an expressJS webserver that is still in the works. To prevent displaying the entire program on the webserver itself, I have learned about the possib ...

Error: The gulp-cssmin plugin encountered a TypeError because it attempted to read the property '0' of a null

I am attempting to condense my code, but I am encountering this error: D:\gulp-compiler\node_modules\gulp-cssmin\node_modules\clean-css\lib\selectors\extractor.js:66 return name.replace(/^\-\w+\-/, ...

Turn off the interconnected route while utilizing npm to display the tree of dependencies

If I want to display the project's dependencies tree using npm, I would use the following command: npm ls lodash The output will look something like this: > npm ls lodash npm info using [email protected] npm info using [email protected] ...

What strategies can I use to ensure that I can successfully send 3,000 requests to the Google Drive API using node.js without surpassing

I'm currently assisting a friend with a unique project he has in mind. He is looking to create 3000 folders on Google Drive, each paired with a QR code linking to its URL. The plan is to populate each folder with photos taken by event attendees, who ...

Console displays 'undefined' when using the post method with Node.js/Express

Below is the code for my upload.ejs page: <%- include('header' ,{ title:"Playground" }) -%> <div class="wrapper"> <form action="/upload" method="POST" enctype="multipart/form-data"> <input type="text" name="name" place ...

Having trouble getting MongoDB to connect correctly to my server (using node.js and express), any help would be greatly appreciated

Whenever I make an API request using Hoppscotch, I encounter a terminal error along with status code 500 on Hoppscotch. Despite my efforts to fix the issue, it persists. Below is the code snippet that I am currently working with: var mongodb = require(&apo ...