-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
123 lines (104 loc) · 3 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
//require all the modules used
const express= require("express");
const bodyParser= require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");
const bcrypt = require('bcrypt');
const saltRounds = 10;
const session = require('express-session');
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const https= require("https");
// create an express app
const app= new express();
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended:true
}));
app.use(session({
secret: "this is white board",
resave: false,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.set('strictQuery', false);
mongoose.connect("mongodb://0.0.0.0:27017/boardDB",{
useNewUrlParser: true,
useUnifiedTopology: true,
// family: 4,
});
const boardSchema = new mongoose.Schema({
email:String,
password: String
});
const Board = mongoose.model("Board", boardSchema);
app.get("/",function(req,res){
res.render("index");
})
app.get("/home",function(req,res){
res.render("home")
})
app.get("/signup",function(req,res){
res.render("signup");
});
app.get("/login",function(req,res){
res.render("login");
});
app.post("/signup",async function(req,res){
// Store hash in your password DB.
try{
const foundUser = await Board.findOne({ email: req.body.email });
if(foundUser){
res.render("login", { message: "User already exists! Please log in." });
}
else{
bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
const newUser = new Board({
email: req.body.email,
password: hash
});
newUser.save().then(function(id){
console.log(id+"\nuser signup successful");
res.redirect("/login");
}).catch(function (err){
console.log(err);
});
});
}
}
catch(err){
console.log(err);
res.status(500).json({ message: "Server error" });
}
});
app.post("/login", async function(req,res){
const email = req.body.email;
const password = req.body.password;
try{
const foundUser = await Board.findOne({ email: email });
if(foundUser){
//if the user is found, compare the password entered.
const isPasswordMatch = await bcrypt.compare(password, foundUser.password);
if(isPasswordMatch){
res.redirect("/home")
}
else{
//if the password entered is incorrect, redirect with a message.
res.render('login',{message:"Password entered is incorrect! Please try again."})
}
}
else{
//if the user is not found, render login page with a message to enter password again
res.render('login',{message :"Invalid login id. Please try again, or sign up!"});
}
}
catch(err){
console.log(err);
res.status(500).json({ message: "Server error" });
}
})
app.listen(3000,function(){
console.log("Server started at port 3000");
})