-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
259 lines (229 loc) · 7.55 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import express from "express";
import dotenv from 'dotenv'
dotenv.config();
import bodyParser from "body-parser";
import mongoose from "mongoose";
import session from 'express-session'
import passport from 'passport'
import passportLocalMongoose from 'passport-local-mongoose'
import {Strategy as GoogleStrategy} from 'passport-google-oauth20'
import findOrCreate from 'mongoose-findorcreate'
// import encrypt from "mongoose-encryption"; //level 2 encryption
// import md5 from 'md5'
// import ejs from 'ejs'
// import bcrypt from 'bcrypt'
// const saltRounds = 10;
const app = express();
app.use(express.static('public')); // for rendering dynamic pages
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended: true})); //to get body of data that is entered and tap into it
app.use(session({
secret: "mySecret",
resave: false,
saveUninitialized: false
})); //session started
app.use(passport.initialize()); //starting passport
app.use(passport.session()); //starting session with passport
mongoose.set('strictQuery', true) // to remove deprecation warning
//connection with database
mongoose.connect(`mongodb+srv://yashgarg:${process.env.PASSWORD}@cluster0.fhdwxom.mongodb.net/userDB?retryWrites=true&w=majority`, (err) =>{
if(err){
res.send(`Something went wrong: ${err}`);
}
console.log('db connected');
});
//defining user schema
const userSchema = new mongoose.Schema (
{email: String,
password: String,
googleId: String,
secret: String},
{timestamps: {
createdAt: 'created_at', // Use `created_at` to store the created date
updatedAt: 'updated_at' // and `updated_at` to store the last updated date
}}
);
//for passport local mongoose
userSchema.plugin(passportLocalMongoose);
//creating the findOrCreate function for google oauth strategy
userSchema.plugin(findOrCreate);
//encryption - level 2
// userSchema.plugin(encrypt, { secret: process.env.SECRET, encryptedFields: ['password'] });
//encryption - level 2
//creating a mongoose db model with User as an instance
const User = mongoose.model('Users', userSchema);
//creating a strategy with User instance
passport.use(User.createStrategy());
//turn data into cookie
passport.serializeUser(function(user, cb) {
process.nextTick(function() {
return cb(null, {
id: user.id,
username: user.username,
picture: user.picture
});
});
});
//turn cookie into data
passport.deserializeUser(function(user, cb) {
process.nextTick(function() {
return cb(null, user);
});
});
//oauth2 google strategy
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:4000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function(accessToken, refreshToken, profile, cb) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
console.log(profile); //for checking profile details
return cb(err, user);
});
}
));
//oauth2 google strategy
///////////////////////////ALL THE ROUTES //////////////////////////////////
////////////////////////////////////////////////////////////////////////////
app.get('/', (req, res) => {
res.render('home.ejs');
});
///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//for login and register via oauth
//this opens the page with all your google ids
app.get('/auth/google',
passport.authenticate('google', { scope: ["profile"] })
);
//for redirecting them back to secrets or login in error case
app.get('/auth/google/secrets',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/secrets');
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
app.route('/login')
.get((req, res) => {
res.render('login.ejs')
})
.post((req, res) =>{
//level 3 - hashing
// User.findOne({email: req.body.username }, (err, foundUser) =>{
// if(err){
// res.send(err);
// } else{
// if(foundUser){
// bcrypt.compare(req.body.password, foundUser.password,(err, result) => {
// if(result === true){
// res.render('secrets.ejs')
// }
// res.send(`Something went wrong: ${err}`)
// });
// }
// }
// })
const user = new User({
username : req.body.username,
password: req.body.password
})
req.login(user, function(err) {
if (err) {
console.log(err);
}
//authentication via passport
passport.authenticate('local')(req, res, () => {
res.redirect('/secrets');
})
})
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
app.route('/register')
.get((req, res) => {
res.render('register.ejs');
})
.post((req, res) => {
// bcrypt.hash(req.body.password, saltRounds)
// .then(function(hash) {
// const newUser = new User({
// email: req.body.username,
// password: hash
// });
// newUser.save(() =>{
// res.render('secrets.ejs');
// })
// })
// .catch((err) => res.send(`Something went wrong: ${err}`))
User.register({username: req.body.username}, req.body.password, (err, userData) =>{
if(err){
console.log(err);
}
//authentication via passport
passport.authenticate('local')(req, res, () => {
res.redirect('/secrets')
})
})
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
app.get('/secrets', (req, res) =>{
// if(req.isAuthenticated){
// return res.render('secrets.ejs')
// }
// res.redirect('/login')
User.find({'secert': {$ne : null}}, (err, foundUsers) =>{
if(err){
console.log(err)
}else{
//userWithScrets is an object with all the secrets of users in the database
if(foundUsers){
res.render('secrets.ejs', {usersWithSecrets: foundUsers});
}
}
})
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
app.get('/logout', (req, res) => {
req.logout((err) =>{
if(err){
console.log(err)
}
res.redirect('/')
});
});
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
app.route('/submit')
.get((req, res) =>{
//checking whether the user is loggeg in or not
if(req.isAuthenticated){
return res.render('submit.ejs')
}
res.redirect('/login')
})
.post((req, res) =>{
console.log(req.body.secret)
console.log(req.user);
User.findById(req.user.id, (err, foundUser) =>{
console.log(foundUser);
if(err){
console.log(err);
}else{
if(foundUser){
foundUser.secret = req.body.secret;
foundUser.save(() =>{
res.redirect('/secrets')
})
}
}
})
});
///////////////////////////////////////////////////////////////////////////
app.listen(4000, () =>{
console.log('listening on 4000')
});