-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
107 lines (78 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
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
var passport = require('passport')
, User = require('./models/user')
module.exports = function (app){
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/')
}
//------------------Index-----------------------
app.get('/', function (req, res) {
res.render('index', {
title: 'Sign up with Twiiter'
});
})
app.get('/logout', function (req, res){
req.logout();
res.redirect('/');
});
//-------------------Login------------------------
app.get('/login', function (req, res) {
res.render('login', {
title: 'Login'
});
});
app.post('/login', passport.authenticate('local'),
function (req, res) {
var accessToken;
User.findOne({ userId: req.user, pass: req.body.password}, function (err, doc){
if(err){
console.log(err);
res.redirect('/login')
}
if(doc){
accessToken = doc.accessToken;
req.session.accessToken = accessToken;
req.session.userId = req.user;
res.redirect('/account')
}
});
});
//--------------------Signup-----------------------
app.get('/signup', ensureAuthenticated, function (req, res) {
res.render('signup', {
title: 'Signup'
});
})
app.post('/signup', function (req, res) {
console.log('user = ' + req.user.id);
handle = req.body.handle;
pass = req.body.password1;
email = req.body.email;
User.findOne({userId: req.user.id}, function (err, doc) {
if(err){
console.log(err);
res.redirect('/');
}
else {
doc.handle = handle;
doc.pass = pass;
doc.email = email;
doc.save();
res.redirect('/account');
}
})
})
//--------------------Oauth--------------------------
app.get('/auth/twitter',
passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/signup');
});
//--------------------Account----------------------------
app.get('/account', ensureAuthenticated, function(req, res) {
res.render('account');
})
}