如何为每个用户设置唯一的 URL
How to have a unique url for every user
路由的新代码.js和login.ejs:
`module.exports = function(app, passport) {
// =====================================
// HOME PAGE (with login links) ========
// =====================================
app.get('/', function(req, res) {
res.render('./pages/index.ejs'); // load the index.ejs file
});
// =====================================
// LOGIN ===============================
// =====================================
app.get('/login', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('./pages/login.ejs', { message: req.flash('error') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true
}));
// =====================================
// SIGNUP ==============================
// =====================================
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('./pages/signup.ejs', { message: req.flash('signupMessage') });
});
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
// =====================================
// PROFILE SECTION =========================
// =====================================
// we will want this protected so you have to be logged in to visit
// we will use route middleware to verify this (the isLoggedIn function)
app.get('/profile/:id', isLoggedIn, function (req, res) {
var id = req.params.id;
res.send('./pages/profile.ejs' + req.params.id);
});
// =====================================
// LOGOUT ==============================
// =====================================
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// =====================================
// UPLOAD ==============================
// =====================================
app.get('/upload', function (req, res){
res.render('./pages/upload.ejs');
});
// =====================================
// PASSWORD RESET ======================
// =====================================
app.get('/forgot', isLoggedIn, function (req, res){
res.render('./pages/forgot.ejs');
});
app.post('/forgot', function(req, res, next) {
async.waterfall([
function(done) {
crypto.randomBytes(20, function(err, buf) {
var token = buf.toString('hex');
done(err, token);
});
},
function(token, done) {
User.findOne({ email: req.body.email }, function(err, user) {
if (!user) {
req.flash('error', 'No account with that email address exists.');
return res.redirect('/forgot');
}
user.resetPasswordToken = token;
user.resetPasswordExpires = Date.now() + 3600000; // 1 hour
user.save(function(err) {
done(err, token, user);
});
});
},
function(token, user, done) {
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'SendGrid',
auth: {
user: '!!! YOUR SENDGRID USERNAME !!!',
pass: '!!! YOUR SENDGRID PASSWORD !!!'
}
});
var mailOptions = {
to: user.email,
from: 'passwordreset@demo.com',
subject: 'Node.js Password Reset',
text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.'n'n' +
'Please click on the following link, or paste this into your browser to complete the process:'n'n' +
'http://' + req.headers.host + '/reset/' + token + ''n'n' +
'If you did not request this, please ignore this email and your password will remain unchanged.'n'
};
smtpTransport.sendMail(mailOptions, function(err) {
req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
done(err, 'done');
});
}
], function(err) {
if (err) return next(err);
res.redirect('/forgot');
});
});
};
// route middleware to make sure
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if(req.isAuthenticated()){
return next();
}
// if they aren't redirect them to the home page
else{
res.redirect('/');
}
}`
和视图(login.ejs):
`<!DOCTYPE html>
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-sign-in"></span>Login</h1>
<% if(message.length > 0){ %>
<div class="alert alert-danger"><%= message %></div>
<% } %>
<!-- LOGIN FORM -->
<form action="/login" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Login</button>
</form>
<hr>
<p>Need an account? <a href="/signup">Signup</a></p>
<p>Forgot your or password? <a href="/forgot">Forgot</a></p>
</div>
</div>
</body>
</html>`
我也可以像 = http://localhost:8080/profile/emailAddress
.
电子邮件地址是在注册我的网站时给出的。电子邮件地址存储在我设置的数据库中。
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.
使用附加参数创建新路由
app.get('/profile/:id', function (req, res) {
var id = req.params.id
res.render('./pages/profile.ejs', {user: id});
})
您应该使用请求参数。使用快速,您可以执行以下操作:
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});
});
在你的isLoggedIn
中间件中,你会有这样的东西:
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
}
}
在您的视图中,您将使用用户的 id 来构建 url:
<a href="/profile/<%=user.id%>"><%=user.name%></a>
相关文章:
- 如何设置html元素填充的动画
- 需要帮助设置json数组
- 为dropdownlistfor(MVC)中的每个元素设置唯一id
- “new”关键字是在构造对象时自动设置“constructor”属性的唯一方法吗
- 使用 addEventListener 在 JavaScript 中设置单独的函数参数以具有唯一的 ID
- 如何为每个用户设置唯一的 URL
- 根据内容高度为多个iFrame设置唯一高度
- Sequelize:无法设置新的唯一约束消息
- 我应该使用Javascript或PHP设置一个全局变量来唯一标识web应用程序中的每个页面吗
- 为表中要存储在localStorage中的选定行设置唯一ID
- 在实例化对象(使用构造函数)时设置唯一的id
- 如何在javascript中实现唯一的键值设置
- 在JavaScript中以编程方式设置唯一的对象ID
- 如何使用javascript设置多个标签的唯一id
- 获取/设置leafletjs圆圈标记的唯一id
- Angularjs的输入[radio]带有唯一的ng-model,不能取消设置
- ReactJS为可吸收列表元素设置唯一键
- 如何使用JavaScript在mongodb中设置唯一id
- CSS是设置HTML文档样式的唯一方法吗
- 如何将一个属性的唯一计数设置为另一个属性