-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
79 lines (71 loc) · 2.12 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
const passport = require('passport');
const bcrypt = require('bcrypt');
module.exports = function(app, myDataBase) {
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/');
}
app.route('/login').post( passport.authenticate('local', {failureRedirect: '/'}), (req, res) => {
res.redirect('/chat');
});
app.route('/profile').get(ensureAuthenticated, (req, res) => {
res.render(process.cwd() + '/views/pug/profile', {username: req.user.username});
});
app.route('/auth/github')
.get(passport.authenticate('github'), (req, res) => {
res.redirect('/profile');
})
app.route('/auth/github/callback')
.get(
passport.authenticate('github', {failureRedirect: '/'}), (req, res) => {
req.session.user_id = req.user.id
res.redirect('/chat');
}
);
app.route('/').get((req, res) => {
res.render('pug', {
title: 'Connected to Database',
message: 'Please login',
showLogin: true,
showRegistration: true,
showSocialAuth: true
});
});
app.route('/logout')
.get((req, res) => {
req.logout();
res.redirect('/');
});
app.route('/register')
.post((req, res, next) => {
myDataBase.findOne({ username: req.body.username}, function(err, user) {
if (err) {
next(err);
} else if (user) {
res.redirect('/');
} else {
const hash = bcrypt.hashSync(req.body.password, 12);
myDataBase.insertOne({
username: req.body.username,
password: hash
}, (err, doc) => {
if (err) {
res.redirect('/');
} else {
// The inserted doc is held
// within the ops prop of the doc
next(null, doc.ops[0]);
}
})
}
})
}, passport.authenticate('local', { failureRedirect: '/'}), (req, res, next) => {
res.redirect('/profile');
});
app.route('/chat')
.get(ensureAuthenticated, (req, res) => {
res.render(process.cwd() + '/views/pug/chat');
});
}