forked from NCKUKIWI/CHUB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
95 lines (88 loc) · 2.05 KB
/
helper.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
88
89
90
91
92
93
94
95
var config = require("./config");
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: config.email.sender,
pass: config.email.pw
}
});
exports.handleError = function handleError(err) {
var errmsg = [];
for(var i in err.errors) {
errmsg.push(err.errors[i].message);
}
return errmsg;
}
exports.removeFromArray = function removeFromArray(arr,element) {
var index = arr.indexOf(element);
if(index!=-1){
arr.splice(index,1);
}
return arr;
}
exports.sendEmail = function sendEmail(toEmail,subject,text){
var options = {
from: `"CHUB" ${config.email.sender}`,
to:toEmail,
subject:subject,
text:text
};
transporter.sendMail(options, function(error, info){
if(error){
console.log(error);
}else{
console.log('訊息發送: ' + info.response);
}
});
}
exports.apiAuth = function apiAuth(){
return function(req, res, next) {
if (req.user) {
next();
} else {
res.send({error:"notLogin"});
}
}
}
exports.noinjection = function noinjection(req){
for(var i in req.body){
if(typeof(req.body[i] == 'object')){ // 避免有array的情形
for(var j in req.body[i]){
req.body[i][j] = req.body[i][j].replace(/\'|\#|\/|\*|\{|\}|\:/g,"");
}
}
else {
req.body[i] = req.body[i].replace(/\'|\#|\/|\*|\{|\}|\:/g,"");
}
}
for(var i in req.query){
if(typeof(req.query[i] == 'object')){ // 避免有array的情形
for(var j in req.query[i]){
req.query[i][j] = req.query[i][j].replace(/\'|\#|\/|\*|\{|\}|\:/g,"");
}
}
else {
req.query[i] = req.query[i].replace(/\'|\#|\/|\*|\{|\}|\:/g,"");
}
}
}
exports.checkLogin = function checkLogin(v){
if(v === undefined){
return function(req, res, next) {
if(req.user) {
next();
} else {
res.redirect("back");
}
}
}else{
return function(req, res, next) {
if(!req.user) {
next();
} else {
res.redirect("back");
}
}
}
}