-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
169 lines (147 loc) · 4.11 KB
/
server.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 express = require('express');
var passport = require('passport');
const hbs = require('hbs');
var bodyParser = require('body-parser');
var session = require('express-session');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var {Post} = require('./app/models/user');
var {mongoose} = require('./app/dbhandler');
// set up our express application
var app = express();
var profile = express();
var port = process.env.PORT || 8080;
app.set('view engine', 'hbs');
// required for passport
app.use(passport.initialize());
app.use(session({ secret: 'secret',saveUninitialized: true,resave: true })); // session secret
app.use(passport.session());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// route for home page
app.use('/', express.static('./'));
app.use('/profile', profile);
function getPost() {
return Post.find().then((docs) => {
resolve(docs);
});
}
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
console.log('serialize');
done(null, user);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
console.log('deserialize');
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use(new GoogleStrategy({
clientID : '726634443799-57gq2cpg1gvhfnobtoiudml1esm6tmsg.apps.googleusercontent.com',
clientSecret : 'iSUiJIf6VOMNcaMEwRBaCnMA',
callbackURL : 'http://localhost:8080/auth/google/callback'
},
function(token, refreshToken, profile, done) {
process.nextTick(function() {
var user = {
name: profile.displayName,
email: profile.emails[0]
}
return done(null, user);
});
}));
// route for login form
// route for processing the login form
// route for signup form
// route for processing the signup form
// route for showing the profile page
// route for logging out
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login', 'profile', 'email'] }));
// the callback after google has authenticated the user
app.get('/auth/google/callback',
passport.authenticate('google', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// GET profile route
app.get('/profile', function(req, res) {
sessionTemp = req.sessionStore.sessions;
sessionTemp1 = sessionTemp[req.sessionID];
var temp = JSON.parse(sessionTemp1).passport.user;
var tempUser = {
name: temp.name,
email: temp.email.value
}
Post.find().then((docs) => {
return docs;
}).then( (docs) => {
res.render('profile.hbs', {
name: tempUser.name,
data: docs
});
});
});
//POST the post
app.post('/postData', (req, res) => {
var user = new Post({
text: req.body.postData,
name: req.body.name
});
user.save().then((doc) => {
});
res.send();
});
//GET the post
profile.get('/:id', (req, res) => {
sessionTemp = req.sessionStore.sessions;
sessionTemp1 = sessionTemp[req.sessionID];
var temp = JSON.parse(sessionTemp1).passport.user;
var tempUser = {
name: temp.name,
email: temp.email.value
}
Post.findById(req.params.id).then((data) => {
return data;
}).then((data) => {
if(data.comment == null)
{
res.render('comment.hbs', {
userName: tempUser.name,
name: data.name,
text: data.text,
});
}
var temp = {
commentName: data.comment.commentName,
commentData: data.comment.commentData
}
res.render('comment.hbs', {
userName: tempUser.name,
name: data.name,
text: data.text,
data: data.comment
});
});
});
//comment on the post
profile.post('/postComment', (req, res) => {
var temp = {
commentName: req.body.name,
commentData: req.body.postComment
}
Post.findById(req.body.id).then((doc) => {
doc.comment.push(temp);
doc.save().then((data) => {
res.send();
});
});
});
app.listen(port, () => {
console.log('The magic happens on port ' + port);
});