-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
169 lines (146 loc) · 6.05 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
*
*
**/
var http = require('http')
var express=require("express");
var app=express();
var server = http.createServer(app)
var io =require('socket.io')(server);
var bodyParser=require('body-parser');
var mongoose=require('mongoose');
server.listen(801,"localhost");
var settings=require('./settings.js');
var url =settings.url;
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
var conEnsure=require('connect-ensure-login');
var fs = require('fs');
var socket = require('./socketHandler');
socket.start(io);
//-----routers-------------
var prouter=require('./routes/prouter.js');
var cfrouter=require('./routes/cfrouter.js');
var adminrouter=require('./routes/adminrouter.js');
var eModuleRouteHandler = require('./routes/eModuleRoute');
var moduleRouteHandler = require('./routes/moduleRoute');
var profRouteHandler = require('./routes/profRoute');
var filiereRouteHandler = require('./routes/filiereRoute');
//-----models---------------
//var User=require("./models/User");
var User = require("./models/databaseModels").profs
var Rat=require("./models/rattrappage");
var Matiere=require("./models/Matiere");
var Notes=require("./models/Notes");
var Module=require("./models/Module");
//--------------------------
var datab=null;
var trick=null;
var async = require('async');
mongoose.connect(url);
app.disable("x-powered-by");
app.set('view engine','jade');
app.use(function(req, res, next) { //allow cross origin requests
res.setHeader("Access-Control-Allow-Methods", "POST, PUT, OPTIONS, DELETE, GET");
res.header("Access-Control-Allow-Origin", "http://localhost");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(__dirname+"/public",{index:false}));
app.use('/app',express.static(__dirname+"/public/app"));
app.use('/bower_components',express.static(__dirname+"/public/bower_components"));
app.use('/js',express.static(__dirname+"/public/app/js"));
app.use('/css',express.static(__dirname+"/public/app/css"));
app.use('/dist',express.static(__dirname+"/public/bower_components/jquery/dist"));
app.use('/Settings',express.static(__dirname+"/public/app/Settings"));
app.use('/Gest-Charges',express.static(__dirname+"/public/app/Gest-Charges"));
app.use('/Gest-Delib',express.static(__dirname+"/public/app/Gest-Delib"));
app.use('/Gest-Filiere',express.static(__dirname+"/public/app/Gest-Filiere"));
app.use('/Gest-Scolarite',express.static(__dirname+"/public/app/Gest-Scolarite"));
app.use('/login',express.static(__dirname+"/public/app/login"));
////
var expSession=require('express-session'),
MongoStore = require('connect-mongo')(expSession);//the session store.
app.use(expSession({secret: '$zxyz-banana-0987choco', resave: false, saveUninitialized: false ,store:new MongoStore({url:'mongodb://127.0.0.1:27017/test'})}));
app.use(passport.initialize());//initialise le middlware passport
app.use(passport.session()); //l'implementation de la session dans passport utilise express-session
//mettre en place la strategie d'authentification passport
passport.use(new LocalStrategy(/*passReqToCallback:true*/{
usernameField: 'login',
passwordField: 'password'
},
function(username,password,done) {
User.findOne({login: username,password:password}, function(err, user){
if (err) { return done(err);}
else if (!user)
return done(null, false, {message: 'Incorrect username.'});
else if (user.password!=password)
return done(null, false, {message: 'Incorrect password.'});
else{
return done(null, user);
}
});
}));
//=========================================
//sauvgarde des infos d'user authentifié dans le session-store (connect-mongo)
passport.serializeUser(function(user,done){
done(null,user._id);
});
//recupère les infos(id+login+security_mask) d'un à partir de la session
passport.deserializeUser(function(id, done) {
User.findById(id,'_id login security_mask filiere active_semestre nom',function(err,user){
if(!err)done(null,user);
});
});
app.use(bodyParser.urlencoded({limit:128/*limiter la taille du body par securité*/,extended:true}));
app.use(bodyParser.json());
//=======login_===========
app.get('/login_',function(req,res){
res.status(200).json({info:"non_auto"});
});
//=======installing routes===========
app.use('/',prouter);
app.use('/',cfrouter);
app.use('/',adminrouter);
app.use('/gestionfiliere/eModules',eModuleRouteHandler);
app.use('/gestionfiliere/modules',moduleRouteHandler);
app.use('/gestionfiliere/profs',profRouteHandler);
app.use('/gestionfiliere/filiere',filiereRouteHandler);
//================================
/* app.get("/login",function(req,res){
res.sendFile(__dirname+"/public/index.html");
}); */
app.post('/login',function(req,res) {
passport.authenticate('local',function(err,user,info){
if(err){
res.status(404).json(err);
return;
}
else if(!user){
res.status(401).json({err:info});
}else {
req.logIn(user,function(err){
if(err) next(err);
else res.status(200).json({ok:"success"});
});
}
})(req,res);
});
// app.get('/welcom',conEnsure.ensureLoggedIn(0,"/login"),function(req,res){
// console.log(JSON.stringify(req.user));
// res.render("welcom",{user:req.user.name});
// });
//==============outil: js global trim====================================================
var white= new RegExp(/(\s|[^A-Za-z0-9_-]|[\n\r])/g);
String.prototype.gtrim=function(){
return this.replace(white,'');
};
//==================================fermer la session=====================================
app.get('/logout',function(req,res){
req.logout();
res.status(200).json({ok:"logged_out"});
});
//-------front-end routes
app.get("*",function(req,res){
res.sendFile("/public/app/index.html",{root:__dirname});
});