forked from alexisbriandev/bootstrap-simple-admin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
168 lines (144 loc) · 4.82 KB
/
app.js
File metadata and controls
168 lines (144 loc) · 4.82 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Importing libraries
const { getUser, addUser, getUserIngredients, addIngredient } = require('./database.js')
const express = require('express');
const session = require('express-session');
const path = require('path');
// Initializing app
const app = express();
// Creating an express session for passing user info across pages (caching)
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));
const PORT = process.env.PORT || 3300;
// Middleware
// For loading in css and bootstrap
app.use('/assets', express.static(path.join(__dirname, 'assets')));
// For parsing incoming requests
app.use(express.urlencoded({extended: false}));
// Routes
// For handling registration form submission
app.post("/register", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
getUser(username, password)
.then(existingUser => {
if (existingUser === undefined ) {
addUser(username, password)
.then(newUser => {
if (newUser === 1){
console.log("User created");
res.redirect("/login");
}
else {
console.error("Failed to create user");
res.redirect("/register-error");
}
})
}
else {
console.log("User already exists");
res.redirect("/register-error");
}
})
})
// For handling login form submission
app.post("/login", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
getUser(username, password)
.then(existingUser => {
if (existingUser === undefined ) {
console.log("Incorrect credentials. Try again.");
res.redirect("/login-error");
}
else {
console.log("Successfully logged in!");
req.session.user = existingUser;
res.redirect("/home"); // or index
}
})
})
// For handling the add ingredient form submission
app.post("/add-ingredient", async (req, res) => {
const name = req.body.ingredientName;
const user = req.session.user;
if (user === undefined) {
console.log("Not logged in. Cannot add ingredients.");
res.redirect("/login");
}
else {
addIngredient(name, user.Id)
.then(newIngredient => {
if (newIngredient === 1) {
console.log("Ingredient added for user.");
res.redirect("/add-ingredient");
}
else {
console.error("Failed to add ingredient for user");
res.redirect("/add-ingredient");
}
})
}
})
// Route to handle GET requests to the '/login' endpoint
app.get('/login', (req, res) => {
res.render("login.ejs")
})
// Route to handle GET requests to the '/login-error' endpoint
app.get('/login-error', (req, res) => {
res.render("login-error.ejs")
})
// Route to handle GET requests to the '/register' endpoint
app.get('/register', (req, res) => {
res.render("register.ejs")
})
// Route to handle GET requests to the '/register-error' endpoint
app.get('/register-error', (req, res) => {
res.render("register-error.ejs")
})
// Route to handle GET requests to the '/home' endpoint
app.get('/home', (req, res) => {
const user = req.session.user;
console.log(user);
res.render("home.ejs", { user })
})
// Route to handle GET requests to the '/pantry' endpoint
app.get('/pantry', (req, res) => {
const user = req.session.user;
if (user === undefined){
res.redirect("/login")
}
else {
console.log(user.Username);
getUserIngredients(user.Id)
.then(ingredients => {
console.log(ingredients[0]);
res.render("pantry.ejs", { user, ingredients })
})
}
})
// Route to handle GET requests to the '/profile' endpoint
app.get('/profile', (req, res) => {
const user = req.session.user;
if (user === undefined) {
res.redirect("/login")
}
else {
res.render("profile.ejs", { user } )
}
console.log(user);
})
// Route to handle GET requests to the '/cookbook' endpoint
app.get('/cookbook', (req, res) => {
res.render("cookbook.ejs")
})
// Route to handle GET requests to the '/add-ingredient' endpoint
app.get('/add-ingredient', (req, res) => {
res.render("add-ingredient.ejs")
})
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}/login`);
});