Skip to content

Instantly share code, notes, and snippets.

@indreklasn
Created January 26, 2024 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indreklasn/56f273adf71c7e8aa78d5c967c975d34 to your computer and use it in GitHub Desktop.
Save indreklasn/56f273adf71c7e8aa78d5c967c975d34 to your computer and use it in GitHub Desktop.
const express = require('express');
const app = express();
const session = require('express-session');
// Assuming we have a function to get user data
const getUserById = require('./getUserById');
app.use(session({
secret: 'your-secret',
resave: false,
saveUninitialized: true
}));
app.get('/user/:id', (req, res) => {
const userId = req.params.id;
const loggedInUserId = req.session.userId;
// Check if logged-in user ID matches the requested user ID
if (userId === loggedInUserId) {
getUserById(userId, (err, user) => {
if (err) {
return res.status(500).send('Internal Server Error');
}
if (!user) {
return res.status(404).send('User not found');
}
res.json(user);
});
} else {
res.status(403).send('Access Denied');
}
});
app.listen(3000, () => console.log('Server is running on port 3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment