-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
274 lines (182 loc) · 8.24 KB
/
app.js
File metadata and controls
274 lines (182 loc) · 8.24 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// ******** look into the repo for all the authentication versions of this application ********
// What is a Session?
// This series of requests and responses, each associated with the same user, is known as a session.
// In a session, we make a STATEFUL protocol with the use of HTTP Cookies which are transmitted to the server with every request and response of the user.
// The HTTP req and res is a STATELESS protocol meaning every req can be understood in isolation- w/o context of previous req.
// A session is used to maintain a numb of states, one of them are Authentication State, also known as Login State, and to maintain many other unrelated to authentication state.
// What is 'passport'?
// Passport is designed to isolate Authentication State from the other states that may be store in the session.
// session must be initialised using express-session module in order to use make login sessions.
//jshint esversion:6
const dotenv = require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const passport = require('passport'); // authentication middleware for node.js
const passportLocalMongoose = require('passport-local-mongoose'); // this package will automatically salt and hash our passwords automatically w/o us doing it explicitly
const session = require('express-session'); // creates the cookies and stores the contents.
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const findOrCreate = require('mongoose-findorcreate');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(session({
secret: "Our little secret.",
resave: false,
saveUninitialized: false
// session: false
}));
app.use(passport.initialize()); // this is initialisation of passport package
app.use(passport.session());
// app.use(methodOverride("_method"));
app.use(express.static("public"));
mongoose.set('strictQuery', true);
mongoose.connect("mongodb://127.0.0.1/userDB");
const userSchema = new mongoose.Schema({
email: String,
password: String,
googleId: String,
secret: String
});
userSchema.plugin(findOrCreate);
userSchema.plugin(passportLocalMongoose); // this will hash and salt the passwords and save the user into the mongoDB database.
const User = mongoose.model("User", userSchema);
// 'passport-local' configuration ==============
passport.use(User.createStrategy());
passport.serializeUser(function(user, cb) { // serialize creates the cookies and stuffs it with the data namely user's identification into the cookie.
process.nextTick(function() {
return cb(null, {
id: user.id,
username: user.username,
picture: user.picture
});
});
});
passport.deserializeUser(function(user, cb) { // deserialize allows passport to crumbles the cookie and discover the message inside which is who this user is so that we can authenticate them on our server.
process.nextTick(function() {
return cb(null, user);
});
});
// Following is another method to serialize and deserialize:
// passport.serializeUser(User.serializeUser());
// passport.deserializeUser(User.deserializeUser());
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets"
},
function(accessToken, refreshToken, profile, cb) {
User.findOrCreate({ googleId: profile.id }, function (err, user) { // there is no such query as findOrCreate in mongoose, so we found a npm package "mongoose-findorcreate" and installed and required in our poroject.
return cb(err, user);
});
}
));
// '/auth/google' ROUTE ====================================================================================================================
app.get("/auth/google", passport.authenticate("google", { scope: ['profile'] }));
// '/auth/google/secrets' ROUTE ====================================================================================================================
app.get("/auth/google/secrets", passport.authenticate('google', { failureRedirect : "/login"}),
function(req, res){
console.log()
res.redirect("/secrets")
});
// '/' ROUTE ====================================================================================================================
app.get("/", function(req, res){
res.render("home");
});
// LOGIN route ====================================================================================================================
app.get("/login", checkNotAuthenticated, (req, res) => {
res.render("login", {message: ""});
});
app.post("/login", function(req, res){
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function(err){
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req, res, function(){
res.redirect("/secrets");
});
}
});
});
// LOGOUT route ====================================================================================================================
app.post("/logout", (req, res) =>{
req.logout(function(err){
if(err) console.log(err);
else res.redirect("/login");
});
});
// SECRETS route ====================================================================================================================
app.get("/secrets", function(req, res){
User.find({"secret": {$ne: null}}, function(err, foundUsers){
if (err) console.log(err);
else{
if(foundUsers){
res.render("secrets", {SecretArray: foundUsers})
}
}
})
});
// REGISTER route ====================================================================================================================
app.get("/register", checkNotAuthenticated, (req, res) =>{
res.render("register");
});
app.post("/register", (req, res) =>{
User.register({username: req.body.username}, req.body.password, function(err, user){
// user is the new registered user
// the register method comes from the passport-local-mongoose packege and it will save us from creating new user using a vatiable
// and saving using newUser.save(). register() will do it all by itself.
if(err){
console.log(err);
res.redirect("/register");
}
else{
passport.authenticate("local")(req, res, function(){
// this callback will be triggered if the authentication was successful and we managed to successfully setup a cookies that saved the current logged in session.
// upon the authentication, a login session is established
res.redirect("/secrets");
})
}
});
});
// SUBMIT route ====================================================================================================================
app.get("/submit", function(req, res){
if(req.isAuthenticated()){
res.render("submit");
}
else res.redirect("/login", {message: "Login! Authentication Failed!"});
});
app.post("/submit", function(req, res){
const submittedSecret = req.body.secret;
//Once the user is authenticated and their session gets saved, their user details are saved to req.user.
User.findById(req.user.id, function(err, foundUser){
if(err) console.log(err);
else{
if(foundUser){
foundUser.secret = submittedSecret;
foundUser.save(function(){
res.redirect("/secrets");
});
}
}
})
})
// RANDOM functions ====================================================================================================================
function checkNotAuthenticated(req, res, next){
if(req.isAuthenticated()){
return res.redirect("/secrets");
}
else{
next();
}
}
// LISTEN ====================================================================================================================
app.listen(3000, function() {
console.log("Server is live on port 3000");
});