For example I have a page with url "/foo", and I need to display my authenticated users nicknames on this page. Is there any default node.js/passport function for this, or I need to add every user of this page to database and display it with websockets?
User schema:
var userSchema = new Schema({
email: {type: String, required: true, unique: true },
password: {type: String, required: true },
nickname: { type: String, default: 'User'},
createdAt: { type: Date, default: Date.now }
});
Check if user is logged in:
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
req.session.oldUrl = req.url;
res.redirect('/');
}
This is how I solved it in a service that hosts stories. I had to display the number of users and their names on the current story page. I wanted to keep the solution simple enough so I decided to use a simple table in SQL.
In an SQL table with a single column, I stored PAGE_{PAGEID}_USER_{USERID}.
For every page when fetched by the user I added an entry on this table and queried the table column with the regex PAGE_{PAGEID}_USER_(.*) with updatedAt less than 1 minute of the current time.
This gives you the IDs of the users on the current page in the past 1 minute.
PS: The correct way to do this would be to do this very same thing in a Wide Column Store