-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (120 loc) · 4.46 KB
/
index.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
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
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
const port = 3020
const {initializeApp,cert}=require("firebase-admin/app");
const {getFirestore}=require("firebase-admin/firestore");
var key=require("./key.json");
initializeApp({
credential :cert(key),
});
const db=getFirestore();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('home');
})
app.set("view engine","ejs");
app.get('/signup', (req, res) => {
res.render('signup');
})
const axios = require('axios');
app.use(express.static('assets'));
const bcrypt = require('bcryptjs');
app.post('/signupsubmit', async (req, res) => {
const uname = req.body.uname;
const email = req.body.email;
const password = req.body.password;
const cpass = req.body.cpass;
if (password !== cpass) {
return res.status(400).send("Passwords do not match");
}
try {
// Check if user with this email already exists
const userSnapshot = await db.collection('users').where('email', '==', email).get();
if (!userSnapshot.empty) {
return res.status(400).send("User already exists with this email");
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10); // 10 is the saltRounds
// Add the user to the database
await db.collection('users').add({
name: uname,
email: email,
password: hashedPassword // Store hashed password
});
res.render('signin');
} catch (error) {
console.error("Error adding user:", error);
res.status(500).send("Error signing up. Please try again later.");
}
})
async function getInstructions(recipeId) {
try {
const response = await axios.get(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${recipeId}`);
const recipe = response.data.meals ? response.data.meals[0] : null;
return recipe ? recipe.strInstructions : "Instructions not available.";
} catch (error) {
console.error(error);
return "Error fetching instructions.";
}
}
// Route to handle search and display of recipe names
app.post('/search', async (req, res) => {
try {
const query = req.body.query;
const response = await axios.get(`https://www.themealdb.com/api/json/v1/1/search.php?s=${query}`);
const recipes = response.data.meals || [];
//console.log(recipes); // Ensure recipes array exists
res.render('index', { recipes: recipes });
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
// Route to display details of a specific recipe
app.get('/recipe/:id', async (req, res) => {
try {
const id = req.params.id;
const response = await axios.get(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`);
const recipe = response.data.meals ? response.data.meals[0] : null;
if (recipe) {
res.render('recipe', { recipe: recipe, getInstructions: getInstructions }); // Pass the recipe object and getInstructions function to the view
} else {
res.status(404).send('Recipe not found');
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.get('/signin', (req, res) => {
res.render('signin');
})
app.post('/signinsubmit', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
try {
// Check if a user with this email exists
const userSnapshot = await db.collection('users').where('email', '==', email).get();
if (userSnapshot.empty) {
return res.send("Login failed: User not found");
}
// Retrieve the user data
const userData = userSnapshot.docs[0].data();
// Compare hashed passwords
const isPasswordMatch = await bcrypt.compare(password, userData.password);
if (isPasswordMatch) {
// Passwords match, authentication successful
res.render('index', { recipes: null, getInstructions: getInstructions });
} else {
// Passwords do not match
res.send("Login failed: Incorrect password");
}
} catch (error) {
console.error(error);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});