-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (58 loc) · 1.89 KB
/
app.js
File metadata and controls
76 lines (58 loc) · 1.89 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
const express = require("express");
const bodtParser = require("body-parser");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const {connection, newUser} = require("./database");
const app = express();
// setting up the body parser middleware and the template engine==================================================
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static('public'));
// "/" route============================================================================================================
app.get("/", (req, res) => {
res.render("signup");
})
app.post("/", async (req, res) => {
const user1 = new newUser({
email: req.body.email,
password: req.body.pass,
admin: req.body.admin
})
await user1.save()
.then((user) => {
console.log(user);
res.redirect("/signin");
})
})
// "/admin" and "/user" route======================================================================================
app.get("/admin", (req, res) => {
res.render("admin");
})
app.get("/user", (req, res) => {
res.render("user");
})
// "/signin" route==============================================================================================
app.get("/signin", (req, res) => {
res.render("signin")
})
app.post("/signin", (req, res) => {
const userEmail = req.body.signinEmail;
const userPass = req.body.signinPass;
newUser.findOne({email:userEmail})
.then((foundUser) => {
if(foundUser.password == userPass){
if(foundUser.admin == "option1"){
res.redirect("/admin");
}
if(foundUser.admin == "option2") {
res.redirect("/user");
}
}
})
.catch((err) => {
console.log(err);
})
})
app.listen(3000, (req, res) => {
console.log("server live");
})