-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
133 lines (108 loc) · 3.31 KB
/
index.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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var User = require("./models/users.js");
var Complaint = require("./models/complaints.js");
var expressSession = require("express-session");
var passport = require("passport");
var LocalStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
var mongoose = require("mongoose");
var nodemailer = require('nodemailer');
// mongoose.connect("mongodb://avadacodavra:[email protected]:25502/shesafe",
// { useNewUrlParser: true }
// )
// .then(() => {
// console.log("Connected to DB");
// });
mongoose.connect("mongodb://localhost/shesafe");
app.use(expressSession({
secret: "My name is Ekansh Bansal",
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()))
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
} else{
res.redirect("/login");
}
}
app.get("/", function(req, res){
res.render("landing")
})
app.get("/secret", isLoggedIn, function(req, res){
res.send("Secret");
})
app.get("/register", function(req, res){
res.render("register");
})
app.post("/register", function(req, res){
User.register(new User({username: req.body.username}), req.body.password, function(err, user){
if(err){
console.log("error at register route");
return res.render("register");
} else{
passport.authenticate("local")(req, res, function(){
res.redirect("/secret");
})
}
})
})
app.get("/login", function(req, res){
res.render("login");
})
app.post("/login", passport.authenticate("local", {
successRedirect: "/secret",
failureRedirect: "/login"
}), function(req,res){
})
app.get("/logout", function(req, res){
req.logout();
res.redirect("/");
});
//==============================================================================
//====================== COMPLAINT ROUTES ==================================
//==============================================================================
app.post("/complaint", function(req, res){
var payload = req.body.complaint;
Complaint.create(payload, function(err, createdComplaint){
if(err){
console.log("Error at the complaint route.");
} else {
console.log(createdComplaint);
//==========================SEND MAIL=====================================
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'switch10books@!'
}
});
var mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.redirect("/");
}
})
})
app.listen(3000, function(){
console.log("Server has started");
})