Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

添加用户登录和退出功能

  • Loading branch information...
commit 8c1eb7cbfbcd93cfa74651f5f338559b9169d77e 1 parent eecbcf0
@dgunzi authored
View
4 app.js
@@ -21,7 +21,9 @@ app.configure(function(){
app.use(express.session({
secret: config.session_secret
}));
-
+ //检测用户中间件
+ app.use(require('./controllers/user').auth_user);
+
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
View
1  config/config.js
@@ -12,6 +12,7 @@ exports.config = {
password : "admin",
node_port : process.argv[2] || 3000,
upload_dir : '/public/uploads/',
+ admins : {admin: true}, //超级管理员
tmp_dir : 'tmp',
db : 'mongodb://127.0.0.1:27017/nodeblog'
}
View
88 controllers/user.js
@@ -107,4 +107,90 @@ exports.add_action = function(req, res, next){
});
});
-}
+}
+
+//login
+exports.login = function(req, res, next){
+ var method = req.method.toLowerCase();
+ if(method === 'get'){
+ res.render('login');
+ return;
+ }
+ if(method === 'post'){
+ var user_name = sanitize(req.body.user_name).trim();
+ user_name = sanitize(user_name).xss();
+ var password = sanitize(req.body.password).trim();
+ password = sanitize(password).xss();
+
+ if(!user_name || !password){
+ res.render('login',{error: '信息不完整'});
+ return;
+ }
+ User.findOne({'user_name': user_name},function(err, userRow){
+ if(err){
+ return next(err);
+ }
+ if(!userRow){
+ res.render('login',{error: '没有此用户,或已被删除'});
+ return;
+ }
+ password = md5(password);
+ if(password != userRow.password){
+ res.render('login',{error: '密码错误'});
+ return;
+ }
+
+ //设置cookie
+ gen_session(userRow, res, req);
+
+ res.redirect('/');
+
+ })
+ }
+}
+
+//logout
+exports.logout = function(req, res, next){
+ req.session.destroy();
+ res.clearCookie(config.auth_cookie_name, {path: '/'});
+ res.redirect('/');
+}
+
+//检测用户中间件
+exports.auth_user = function(req,res,next){
+ if(req.session.user){
+ //如果存在session,直接调用b
+ if(config.admins[req.session.user.user_name]){
+ req.session.user.is_admin = true;
+ }else{
+ req.session.user.is_admin = false;
+ }
+ res.locals.current_user = req.session.user;
+ return next();
+ }else{
+ //如果不存在session,从cookie中调用并设置session
+ var cookie = req.cookies[config.auth_cookie_name];
+ if(!cookie) return next();
+
+ var auth_token = decrypt(cookie, config.session_secret);
+ var auth = auth_token.split('\t');
+ var user_id = auth[0];
+ User.findOne({_id:user_id},function(err,user){
+ if(err) return next(err);
+ if(user){
+ if(config.admins[user.user_name]){
+ user.is_admin = true;
+ }else{
+ user.is_admin = false;
+  }
+ req.session.user = user;
+ req.session.cookie.maxAge = 1000 * 60 * 60;
+ res.locals.current_user = req.session.user;
+ return next();
+ }else{
+ return next();
+ }
+ });
+ }
+};
+
View
19 public/css/custom.css
@@ -163,3 +163,22 @@ img { max-width: 100%;}
.side_bar .login p{
padding:5px 0;
}
+.pull-left{
+ float:left;
+}
+#user_login{
+ height:50px;padding-left:15px;
+}
+#user_login ul{
+ text-align: left;margin-left: 10px;
+}
+#user_login ul li b{
+ background: #C8CFE0;
+ padding: 0 5px;
+ margin-left: 5px;
+ color: #fff;
+ -webkit-border-radius: 3px;
+ -moz-border-radius: 3px;
+ border-radius: 3px;
+}
+#footer{text-align:center;}
View
3  routes.js
@@ -39,6 +39,9 @@ module.exports = function (app) {
app.get('/user_add', user.add_html);
app.post('/useradd', user.add_action);
+ app.get('/login', user.login);
+ app.post('/login', user.login);
+ app.get('/logout', user.logout);
//view
app.get('/:post', cookbook.cookbook_view);
View
2  views/common/footer.html
@@ -1,4 +1,4 @@
- <footer>
+ <footer id="footer">
<p><%=site.copyright%></p>
</footer>
</div>
View
23 views/common/header.html
@@ -21,17 +21,20 @@
<div class="nav-collapse">
<ul class="nav">
<li><a href="/">首页</a></li>
- <li><a href="#contact">联系</a></li>
+ <% if(locals.current_user){ %>
+ <li><a href="/cookbook">菜谱管理</a></li>
+ <li><a href="/logout">账户设置</a></li>
<li><a href="/write">发布菜谱</a></li>
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown">网站管理 <b class="caret"></b></a>
- <ul class="dropdown-menu">
- <li><a href="/cookbook">菜谱管理</a></li>
- <li><a href="/user">新菜谱</a></li>
- <li><a href="/logout">个人账户</a></li>
- </ul>
- <li><a href="#about">关于</a></li>
- </li>
+ <li><a href="/logout">退出登录</a></li>
+ <%
+ }else{
+ %>
+ <li><a href="/login">登录</a></li>
+ <%
+ }
+ %>
+ <li><a href="/about">关于</a></li>
+
</ul>
<form class="navbar-search pull-left" action="">
<input type="text" class="search-query span2" style="height:25px;line-height:25px" placeholder="搜索">
View
4 views/cookbookwrite.html
@@ -38,8 +38,8 @@
<div class="span3">
<ul class="nav nav-pills nav-stacked">
<li><a href="/cookbook">菜谱管理</a></li>
- <li class="active"><a href="/write">新菜谱</a></li>
- <li><a href="#">个人账户</a></li>
+ <li class="active"><a href="/write">发布菜谱</a></li>
+ <li><a href="#">账户设置</a></li>
</ul>
</div>
<div class="span9">
View
23 views/index.html
@@ -67,6 +67,26 @@
</ul>
</div>
<div class="span4 side_bar">
+ <%
+ if(locals.current_user){
+ %>
+ <section class="login thumbnail" id="user_login">
+ <div class="pull-left user_icon">
+ <a href="/user_view/<%= current_user.user_name %>" class="btn btn-info">
+ <i class="icon-user icon-white"></i>
+ <span><%= current_user.user_name%></span>
+ </a>
+ </div>
+ <ul class="pull-left unstyled">
+ <li>
+ 发表的菜谱:<b>12</b>
+ </li>
+ <li>
+ 发表的回复:<b>79</b>
+ </li>
+ </ul>
+ </section>
+ <% }else{ %>
<section class="login thumbnail">
<h3>Homely Dishes,欢迎您!</h3>
<a href="/user_add" class="btn btn-info">注册</a>
@@ -75,6 +95,9 @@
<a href="/login">登录</a>
</p>
</section>
+ <%
+ }
+ %>
<section class="thumbnail">
<div class="page-header">
<h2>大家在看</h2>
View
84 views/login.html
@@ -0,0 +1,84 @@
+<%- include common/header.html -%>
+<div class="mainContainer">
+ <style type="text/css">
+ .loginBox{
+ width:360px;height:330px;
+ padding:15px 15px 0 25px;
+ border:1px solid #CCC;
+ color:#000;
+ margin-top:40px;
+ border-radius:8px;
+ background: white; font:11px/1.5em 'Microsoft YaHei' ;
+ position: absolute;
+ left:50%;top:50%;
+ margin-left:-210px;
+ margin-top:-205px;
+ -moz-box-shadow:2px 2px 10px #ccc;
+ -webkit-box-shadow:2px 2px 10px #ccc;
+ box-shadow:2px 2px 10px #ccc;
+ }
+ .loginBox h2{height:45px;font-size:24px;font-weight:600;}
+ .loginhelp{text-align:center;padding-top:15px;}
+ .loginhelp span{display:block;}
+ #loginboxPanel .input-prepend{
+ margin-bottom:15px;
+ }
+ #loginboxPanel .input-prepend .add-on,#loginboxPanel .input-append .add-on{padding:10px 12px;}
+ #loginboxPanel .btns{
+ border-bottom:1px solid #ccc;padding-bottom:25px;
+ }
+ #loginboxPanel .btns .span1{
+ margin-left:30px;
+ }
+ #loginboxPanel .btns .span8{
+ margin-top:12px;
+ }
+ #loginboxPanel .btns .span8 span{
+ padding-left:5px;
+ }
+ #loginboxPanel .tip{
+ margin-bottom:25px;font-size:14px;
+ }
+ #loginboxPanel input.span3{
+ font-size:14px;
+ font-weight:600;
+ width:300px;
+ }
+ #footer{text-align:center;z-index:999; position:fixed; bottom:0; left:0; width:100%; height:80px; line-height:40px;/* for ie6 */ _position:absolute; _top: expression(documentelement.scrolltop + documentelement.clientheight-this.offsetheight);overflow:visible; }
+ </style>
+ <%if(locals.error){%>
+ <div class="alert alert-error">
+ <a class="close" data-dismiss="alert" href="#">x</a>
+ <%= error %>
+ </div>
+ <%}%>
+ <section class="loginBox">
+ <form id="loginform" name="loginform" method="post">
+ <section id="loginboxPanel">
+ <h2>登 录</h2>
+ <div class="tip">请使用已注册账户登录:</div>
+ <div class="input-prepend">
+ <span class="add-on"><i class="icon-user"></i></span><input class="span3" type="text" name="user_name" placeholder="用户名" style="margin:0;padding:19px;">
+ </div>
+ <div class="input-prepend">
+ <span class="add-on"><i class="icon-lock"></i></span><input class="span3" type="password" name="password" placeholder="&nbsp;&nbsp;&nbsp;" style="margin:0;padding:19px;">
+ </div>
+ <section class="row-fluid btns">
+ <section class="span8 lh30"><input type="checkbox" name="rememberme" /><span>下次自动登录</span></section>
+ <section class="span1"><input type="submit" value=" 登 录 " class="btn btn-success btn-large"></section>
+ </section>
+ <section class="loginhelp">
+ <span>您还没有账户吗?<a href="/user_add">去注册一个</a></span>
+ <span><a>忘记密码?</a>| <a href="/">回到主页</a></span>
+ </section>
+ </section>
+ </form>
+ </section>
+ </div>
+ <footer id="footer">
+ <hr/>
+ <p><%=site.copyright%></p>
+ </footer>
+ </div>
+</body>
+</html>
View
2  views/user_add.html
@@ -54,7 +54,7 @@ <h2 class="pull-left"><i class="icon-user"></i>注册</h2>
</div>
</form>
</div>
- <div class="span4">
+ <div class="span4 side_bar">
sdfsdf
</div>
</div>
Please sign in to comment.
Something went wrong with that request. Please try again.