This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (90 loc) · 3.2 KB
/
app.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
96
97
98
99
100
101
102
103
var express = require('express');
//var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var error = require('./node_scripts/helper/error');
var helper = require('./node_scripts/helper/helper');
var database = require('./node_scripts/database/mySQL');
var base = require('./node_scripts/routes/base');
var api = require('./node_scripts/routes/api');
var apiV1 = require('./node_scripts/routes/v1/api');
var preferences = require('./node_scripts/routes/v1/preferences');
var users = require('./node_scripts/routes/v1/users');
var registration_ids = require('./node_scripts/routes/v1/registration_ids');
var projects = require('./node_scripts/routes/v1/projects');
var milestones = require('./node_scripts/routes/v1/milestones');
var auth = function (req, res, next) {
var auth;
// check whether an autorization header was send
if (req.headers.authorization) {
// only accepting basic auth, so:
// * cut the starting "Basic " from the header
// * decode the base64 encoded username:password
// * split the string at the colon
// -> should result in an array
auth = new Buffer(req.headers.authorization.substring(6), 'base64').toString().split(':');
var email = auth[0];
var password = auth[1];
}
if(auth && email && password){
database.user.getByEmailAndPassword(email,password,function(err, user){
if(!err){
// wenn user valide, dann stehen seine daten im result
if(user.email == email && user.password == password){
req.callingUser = {
user_id:user.user_id,
user_name:user.user_name
};
next()
}else{
helper.sendResponse(res,error.getUnauthorizedError());
}
}else{
helper.sendResponse(res,error.getUnauthorizedError());
}
});
}else{
helper.sendResponse(res,error.getUnauthorizedError());
}
};
var app = express();
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/',base);
app.use('/api',api);
app.use('/api/v1', apiV1);
app.use('/api/v1/preferences',preferences);
app.use('/api/v1/secured',auth);
app.use('/api/v1/secured/users', users);
app.use('/api/v1/secured/registration_ids', registration_ids);
app.use('/api/v1/secured/projects', projects);
app.use('/api/v1/secured/milestones', milestones);
// catch 404 and forward to error handler
app.use('*',function(req, res, next) {
helper.sendResponse(res,error.getNotFoundError());
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.code || 500);
res.send({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.code || 500);
res.send({
message: err.message,
error: {}
});
});
module.exports = app;