-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.js
87 lines (77 loc) · 2.65 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var passport = require('passport')
, login = require('connect-ensure-login')
, UserModel = require('./db/mongoose').UserModel
, PublishingHouseModel = require('./db/mongoose').PublishingHouseModel
, fs = require('fs');
exports.index = function(req, res) {
var username = "";
if (req.isAuthenticated())
username = req.user.username;
var ejs = require('ejs'),
templateString = fs.readFileSync(__dirname + '/views/user.ejs', 'utf-8'),
html = ejs.render(templateString, {username : username}),
file = fs.readFileSync(__dirname + '/public/mainHTML.html');
res.end(html + file);
};
exports.loginForm = function(req, res) {
res.render('login', {message: req.flash('error')});
};
exports.authForm = function(req, res) {
PublishingHouseModel.find(function (err, publishHouse) {
if (!err) {
res.render('auth', { publishHouse: publishHouse });
} else {
res.statusCode = 500;
return res.send({error: 'Ошибка сервера'});
}
})
};
exports.signup = function(req, res) {
UserModel.findOne({username: req.body.username}, function (err, user) {
if(user) {
res.statusCode = 422;
return res.send({ error: 'Пользователь с таким именем уже существует' });
} else {
var user = new UserModel({ username: req.body.username, password: req.body.password });
PublishingHouseModel.findOne({name: req.body.selectHouse}, function (err, house) {
if (err) {
res.statusCode = 500;
return res.send({error: 'Ошибка сервера'});
} else {
house.users.push(req.body.username);
house.save(function(err, user) {
if(err) {
res.statusCode = 500;
return res.send({error: 'Ошибка сервера'});
}
});
user.save(function(err, user) {
if(err) {
res.statusCode = 500;
return res.send({error: 'Ошибка сервера'});
}
});
}
})
res.statusCode = 200;
return res.send({status: 'Пользователь сохранен'});
}
})
};
exports.login =
passport.authenticate('local', { successReturnToOrRedirect: '/account', failureRedirect: '/login', failureFlash: true });
exports.logout = function(req, res) {
req.logout();
res.statusCode = 200;
return res.send({status: 'OK'});
}
exports.account = [
function(req, res) {
if (req.isAuthenticated())
res.render('account', { user: req.user });
else {
res.statusCode = 401;
res.send({ error: 'Доступ запрещен' });
}
}
]