-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
84 lines (71 loc) · 2.31 KB
/
server.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
const express = require("express")
const app = express();
var path = require('path');
const bcrypt= require('bcrypt');
const collection = require("./public/javascripts/configMongo")
//Setting up View Engine
app.set('view engine', 'ejs')
//Path Defined
app.get("/", (req, res) => {
res.render("form.ejs",{
accountHolder: req.currentusername
});
})
app.get("/login", (req, res) => {
res.render("login.ejs", {message: ""});
})
app.get("/register", (req, res) => {
res.render("register.ejs", {message: ""});
})
app.get("/home", (req, res) => {
res.render("home.ejs");
})
app.get("/about", (req, res) => {
res.render("about.ejs");
})
//Coverting Data into .json
app.use(express.json());
app.use(express.urlencoded({extended: false}));
/********************* Register User (MongoDB) *********************/
app.post("/register", async (req, res)=>{
const data= {
name: req.body.name,
email: req.body.email,
password: req.body.password
}
//check for existing User
const existingUser = await collection.findOne({email: data.email});
if(existingUser){
res.render('register.ejs', { message: 'Email already in use' });
}else if( data.email=="" || data.name=="" || data.password==""){
res.render('register.ejs', { message: 'Please Fill up' });
}else{
//Hashing Password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(data.password, saltRounds);
data.password= hashedPassword;
const userData = await collection.insertMany(data);
res.redirect("/login");
}
});
/********************* Login User (MongoDB) *********************/
app.post("/login", async (req, res)=>{
try {
const check = await collection.findOne({email: req.body.email});
if(!check){
res.render('login.ejs', {message: 'User not found'});
}
//Check Password
const isPasswordMatch = await bcrypt.compare(req.body.password, check.password);
if(isPasswordMatch){
res.redirect('/home');
}else{
res.render('login.ejs', {message: 'Incorrect Password'});
}
} catch {
res.render('login.ejs', {message: 'Wrong Details'});
}
});
//Static Path
app.use(express.static(path.join( __dirname, "public")));
app.listen(3000)