How To Have A Unique Url For Every User
New code for routes.js and login.ejs: `module.exports = function(app, passport) { // ===================================== // HOME PAGE (with login links) ======== //
Solution 1:
app.get('/profile', isLoggedIn, function (req, res) {
// store userId on login into session or any global variable
var userId = req.session.userId
res.redirect('/profile/'+userId)
}); // =>directs to http://localhost:8080/profile for every signup.
Create new route with addition param
app.get('/profile/:id', function (req, res) {
var id = req.params.id
res.render('./pages/profile.ejs', {user: id});
})
Solution 2:
You should use request parameters. With express you can do the following:
app.get('/profile/:id', isLoggedIn, function (req, res) {
var id = req.params.id;
//do with id whatever you want
res.render('./pages/profile.ejs', {user: req.user});
});
In your isLoggedIn
middleware you will have something like this:
function(req, res, next) {
if (isLoggedIn) { //check if the user is logged in
req.user = user; //fetch the user from the DB or wherever you have it
}
}
In your view you will use the user's id to build the url:
<a href="/profile/<%=user.id%>"><%=user.name%></a>
Post a Comment for "How To Have A Unique Url For Every User"