I would like to get the data from session variable (req.user.username) then use it for posting. I'm using passportjs as authentication. Here is my code:
app.use('/login', passport.authenticate("local-register", async (err, user, info) => {
if (err) {
return next('Error');
}
if (!user) {
return next('Error');
}
req.user = user;
return req.login(user, (error: Error) => {
if (error) {
return next('Error');
}
return req.session.save((erro: Error) => {
if (erro) {
return next('Error');
}
return next();
});
});
})(req, res, next);)
app.get('/', async (req, res) => {
console.log(req.user.username) // working just fine
});
app.post('/upload', async (req, res) => {
const uploaderName = req.user.username // I'm getting undefined
const upload = await database.query('INSERT INTO user WHERE username=$1', [uploaderName])
console.log(uploaderName);
})
CodePudding user response:
You could do something like this
app.use((req, res, next) => {
const user = getUserFromSession(req.session);
req.user = user;
next();
});
In this case, if the getUserFromSession() function returns undefined, then req.user will also be undefined.
To fix this, you can make sure that the req.user property is properly set in a previous middleware function, or you can check for undefined and handle the case appropriately in your /upload route handler. For example:
app.post('/upload', async (req, res) => {
const uploaderName = req.user ? req.user.username : null;
if (uploaderName === null) {
res.send('There was an error getting the username.');
return;
}
const upload = await database.query('INSERT INTO user WHERE username=$1', [uploaderName]);
console.log(uploaderName);
});
Alternatively, you could throw an error if req.user or req.user.username is undefined, and catch that error in a global error-handling middleware function. This would allow you to handle the error in a consistent way across all your routes.
app.post('/upload', async (req, res) => {
if (!req.user || !req.user.username) {
throw new Error('There was an error getting the username.');
}
const uploaderName = req.user.username;
const upload = await database.query('INSERT INTO user WHERE username=$1', [uploaderName]);
console.log(uploaderName);
});
app.use((err, req, res, next) => {
res.status(500).send(err.message);
});
CodePudding user response:
So I finally found the answer to the question. For those who will encounter the problem in the future. You just add the session middleware AGAIN on the top of the routes. If your routes are separated to the main server file. /src/routes/routes.ts -> add again the middleware on top.
const app = router();
app.use(sessions) // -> right here you need to add the middleware again to //access the req.user session variable
app.get('/', async (req, res) => {
console.log(req.user.username) // working just fine
});
app.post('/upload', async (req, res) => {
const uploaderName = req.user.username // I'm getting undefined
const upload = await database.query('INSERT INTO user WHERE username=$1', [uploaderName])
console.log(uploaderName);
})