-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
225 lines (185 loc) · 7.09 KB
/
routes.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
var passport = require('passport');
var mongoose = require('mongoose');
var Account = require('./models/account');
//require('./var/passport')(passport); // pass passport for configuration
module.exports = function (app) {
//module.exports = function (app,passport) {
app.get('/', function (req, res) {
// var fs = require('fs');
// var child_process = require('child_process');
// verify that there is NOT any unwanted files in /tmp directory
//if(fs.readdirSync(__dirname+ '/public/tmp/').length > 0 ){
// if so, delete them in a for/in loop
// var badFile = fs.readdirSync(__dirname+ '/public/tmp/');
// for(var x in badFile )
//fs.unlink(__dirname+ '/public/tmp/' + badFile[x]);
// }
res.render('index', { user : req.user });
});
app.get('/register', function(req, res) {
res.render('register', { });
});
app.get('/oups', function(req, res) {
res.render('oups', {message: "You need to use Google Chrome or Chromium to use smartnotes."});
});
app.post('/register', function(req, res,next) {
req.assert('username', 'required').notEmpty();
req.assert('username', 'valid email required').isEmail();
req.assert('password', 'required').notEmpty();
//req.assert('password', '6 to 20 characters required with at least 1 number, 1 upper case character and 1 special symbol').isStrongPassword();
var errors = req.validationErrors();
if (errors) {
return res.render("register", {errors: " Invalid email or password. The password must contain numbers and at least a capital character. "});
}else{
Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) {
var username = req.body.username;
next();
});
//next();
}
//}, passport.authenticate('local-signup', {
//}, passport.authenticate('local-login', {
}, passport.authenticate('local', {
successRedirect : '/home', // redirect to the secure account section
failureRedirect : '/register' ,// redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/home', function(req, res) {
res.render('home', { user : req.user });
});
app.get('/login', function(req, res) {
res.render('login', { user : req.user });
});
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/ping', function(req, res){
res.send("pong!", 200);
});
app.get('/speech.js', function(req, res) {
res.set('Content-Type', 'text/javascript');
res.sendfile('./speech.js');
});
app.get('/data.js', function(req, res) {
res.set('Content-Type', 'text/javascript');
res.sendfile('./data.js');
});
app.get('/controls.js', function(req, res) {
res.set('Content-Type', 'text/javascript');
res.sendfile('./controls.js');
});
// PROFILE SECTION =========================
app.get('/profile', function(req, res) {
res.render('profile', {
user : req.user
});
});
// facebook -------------------------------
// send to facebook to do the authentication
app.get('/connect/facebook', passport.authorize('facebook', { scope : 'email' }));
//app.get('/connect/facebook', passport.authorize('facebook'));
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',
passport.authorize('facebook', {
successRedirect : '/home',
failureRedirect : '/'
}));
// send to facebook to do the authentication
app.get('/auth/facebook', function(req, res, next) {
username = req.body.username;
next();
}, passport.authenticate('facebook', { scope : 'email' }));
//}, passport.authenticate('facebook'));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback', passport.authenticate('facebook', {
successRedirect : '/home',
failureRedirect : '/',
scope : ['email']
}));
// twitter --------------------------------
/*// send to twitter to do the authentication
app.get('/connect/twitter', passport.authorize('twitter', { scope : 'email' }));
// handle the callback after twitter has authorized the user
app.get('/connect/twitter/callback',
passport.authorize('twitter', {
successRedirect : '/home',
failureRedirect : '/'
}));*/
// google ---------------------------------
/*// send to google to do the authentication
app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] }));
// the callback after google has authorized the user
app.get('/connect/google/callback',
passport.authorize('google', {
successRedirect : '/home',
failureRedirect : '/'
}));*/
//=============================================================================
//UNLINK ACCOUNTS =============================================================
//=============================================================================
//used to unlink accounts. for social accounts, just remove the token
//for local account, remove email and password
//user account will stay active in case they want to reconnect in the future
// facebook -------------------------------
app.get('/unlink/facebook', function(req, res) {
var user = req.user;
user.facebook = undefined;
user.save(function(err) {
if (err) {
console.error(err);
}
res.redirect('/profile');
});
});
/*// twitter --------------------------------
app.get('/unlink/twitter', RouteHelpers.isLoggedIn, function(req, res) {
var user = req.user;
user.twitter = undefined;
user.save(function(err) {
res.redirect('/profile');
});
});
// google ---------------------------------
app.get('/unlink/google', RouteHelpers.isLoggedIn, function(req, res) {
var user = req.user;
user.google = undefined;
user.save(function(err) {
if (err) {
console.error(err);
}
res.redirect('/profile');
});
});*/
app.use(function (err, req, res, next) {
// treat as 404
if (err.message
&& (~err.message.indexOf('not found')
|| (~err.message.indexOf('Cast to ObjectId failed')))) {
return next();
}
console.error(err.stack);
// error page
if (req.user) {
// logged in
res.redirect("/home");
} else {
// not logged in
res.status(500).render('500', {error: err.stack});
}
});
// assume 404 since no middleware responded
app.use(function (req, res, next) {
var logged;
if (req.user) {
// logged in
res.redirect("/home");
} else {
// not logged in
res.status(404).render('404', {
url: req.originalUrl,
error: 'Not found'
});
}
});
};