This commit is contained in:
baoliang 2018-01-30 12:26:04 +08:00
parent e7fbc47102
commit 5af4676f53
7 changed files with 88 additions and 37 deletions

55
app.js
View File

@ -6,22 +6,24 @@ var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var fs = require('fs');
var session = require('express-session');
var flash = require('express-flash-2');
if (process.env.NODE_ENV == '' || process.env.NODE_ENV == undefined) {
process.env.NODE_ENV = 'development'
process.env.NODE_ENV = 'development'
} else {
process.env.NODE_ENV = 'production'
process.env.NODE_ENV = 'production'
}
if (process.env.NODE_ENV == 'development') {
console.log('server run as development mode');
global.Connections = require('./config/conn.dev.js');
console.log('server run as development mode');
global.Connections = require('./config/conn.dev.js');
}
if (process.env.NODE_ENV == 'production') {
console.log('server run as production mode');
global.Connections = require('./config/conn.prod.js');
console.log('server run as production mode');
global.Connections = require('./config/conn.prod.js');
}
var app = express();
@ -41,31 +43,44 @@ app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'sjy_eco',
resave: false,
saveUninitialized: true
}));
app.use(flash());
app.use(function(req, res, next) {
res.locals.user = req.session.user;
next();
});
app.use('/', require('./routes/index'));
var routes = fs.readdirSync("../routes/");
for (var route in routes) {
if (routes.hasOwnProperty(route)) {
app.use("/" + routes[route].replace('.js', ''), require('./routes/' + routes[route]));
}
if (routes.hasOwnProperty(route)) {
app.use("/" + routes[route].replace('.js', ''), require('./routes/' + routes[route]));
}
}
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
@ -73,5 +88,5 @@ module.exports = app;
var pg_eco = require('./model/conn/pg_eco');
pg_eco.connTest();
var pg_gis = require('./model/conn/pg_gis');
pg_gis.connTest();
// var pg_gis = require('./model/conn/pg_gis');
// pg_gis.connTest();

View File

@ -15,7 +15,7 @@ exports.getLast10Metadata = function () {
return db.any('select id, uuid, title, description, ts_created from metadata order by ts_created desc limit 10')
};
exports.login = function(name, password) {
}
exports.login = function(username) {
return db.oneOrNone('select id, username, password, usertype, email, realname from users where username = $1', [username])
};

View File

@ -11,10 +11,12 @@
"body-parser": "~1.15.2",
"bootstrap": "^3.3.7",
"cookie-parser": "~1.4.3",
"crypto": "^1.0.1",
"debug": "~2.2.0",
"ejs": "~2.5.2",
"express": "~4.14.0",
"express-promise": "^0.4.0",
"express-session": "^1.15.6",
"geojson": "^0.4.1",
"jquery": "^2.2.4",
"morgan": "~1.7.0",

View File

@ -1,5 +1,6 @@
var express = require('express');
var Promise = require('bluebird');
var crypto = require('crypto');
var router = express.Router();
var models_eco_v1 = require('../model/sjy_eco_v1.js');
@ -10,14 +11,28 @@ router.get('/', function (req, res, next) {
});
});
router.post('/', function(req, res, next) {
if (req.body.username && req.body.username === 'user' && req.body.password && req.body.password === 'pass') {
req.session.authenticated = true;
res.redirect('/secure');
} else {
req.flash('error', 'Username and password are incorrect');
res.redirect('/login');
}
router.post('/', function (req, res, next) {
var username = req.body.username;
var password = req.body.password;
var hashedPassword = crypto.createHash('md5').update(password).digest('hex');
models_eco_v1.login(username).then(function (data) {
console.log(data);
if (data) {
if (data.password === hashedPassword) {
req.session.user = data;
res.redirect('/');
} else {
res.flash('info', '用户名和密码不正确,请输入正确的用户名和密码!');
res.redirect('/login');
}
} else {
res.flash('info', '用户名不存在,请输入正确的用户名!');
res.redirect('/login');
}
}).catch(function (err) {
console.log(err);
});
});
module.exports = router;

12
routes/logout.js Normal file
View File

@ -0,0 +1,12 @@
var express = require('express');
var Promise = require('bluebird');
var router = express.Router();
var models_eco_v1 = require('../model/sjy_eco_v1.js');
router.get('/', function (req, res, next) {
req.session.user = null;
res.redirect('/login');
});
module.exports = router;

View File

@ -33,8 +33,11 @@
<div class="logo-wrapper">
<img src="/images/logo.png" alt="三江源">
<img src="/images/title.png" alt="三江源">
<a href="/logout"><span><i class="anticon anticon-login"></i></span></a>
<a href="/login"><span><i class="anticon anticon-logout"></i></span></a>
<% if(user) {%>
<a href="/logout"><span><i class="anticon anticon-logout"></i></span></a>
<% } else { %>
<a href="/login"><span><i class="anticon anticon-login"></i></span></a>
<% } %>
</div>
<ul class="main-nav-menu navbar-nav">
<li><a aria-current="true" href="/index">首页</a></li>

View File

@ -38,14 +38,18 @@
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<% if (flash.info) { %>
<div class="alert alert-danger" role="alert"><%= flash.info %></div>
<% } else {%>
<div class="alert alert-info" role="alert">请输入用户名和密码登录。如果没有账号,请<a href="http://www.sanjiangyuan.org.cn:4567/account/register">注册</a></div>
<% } %>
</div>
</div>
<form class="form-signin">
<label for="inputEmail" class="sr-only">用户名</label>
<input type="email" id="inputEmail" class="form-control" placeholder="用户名" required autofocus>
<form class="form-signin" method="post" action="/login">
<label for="inputUsername" class="sr-only">用户名</label>
<input type="text" id="inputUsername" name="username" class="form-control" placeholder="用户名" required autofocus>
<label for="inputPassword" class="sr-only">密码</label>
<input type="password" id="inputPassword" class="form-control" placeholder="密码" required>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="密码" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me