-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
99 lines (89 loc) · 2.34 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
const express = require("express"),
app = express(),
mongoose = require("mongoose"),
passport = require("passport"),
bodyParser = require("body-parser"),
LocalStrategy = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
User = require("./database"),
flash = require("connect-flash");
app.set("view engine", "ejs");
app.use(express.static("public"));
mongoose.connect("mongodb://localhost/pdaregister");
app.use(
require("express-session")({
secret: "Any normal Word",
resave: false,
saveUninitialized: false,
})
);
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new LocalStrategy(User.authenticate()));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(passport.initialize());
app.use(passport.session());
app.get("/", function (req, res) {
res.render("home");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.post("/", function (req, res) {
res.redirect("/");
});
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
}),
function (req, res) {}
);
app.post("/register", function (req, res) {
var flag = 0;
var error = new Array();
var name1 = req.body["name"];
var rollnumber = req.body["username"];
var password = req.body["password"];
var cpassword = req.body["cpass"];
var email = req.body["email"];
console.log(name1);
User.register(
new User({
username: req.body.username,
email: req.body.email,
name: req.body.name,
}),
req.body.password,
function (err, user) {
if (err) {
console.log(err);
res.render("login", { error: "user already exists" });
}
passport.authenticate("local")(req, res, function () {
res.render("login", { success: "Successfully registered a new user" });
});
}
);
});
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
});
app.get("/forgotpass", (req, res) => {
res.render("forgot_pass");
});
app.get("/resetpass", (req, res) => {
res.render("reset_pass");
});
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}
app.listen(3000, function () {
console.log("Server is running on port 3000.");
});