-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
170 lines (157 loc) · 4.09 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
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
169
170
const express = require('express');
const app = express();
const port = process.env.Port || 5000;
const db = require('./models');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const HttpError = require('./server_models/http-error');
const checkAuth = require('./middleware/check-auth');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use((error, req, res, next) => {
if (res.headerSent) {
return next(error);
}
res.status(error.code || 500);
res.json({ message: error.message || 'An unknown error occurred.' });
});
// Syncing our sequelize models and then starting our Express app
db.sequelize.sync().then(function() {
app.listen(port, function() {
console.log('App listening on PORT ' + port);
});
});
// ---------- USER ROUTES ----------//
// SIGNUP route for new accounts
app.post('/signup', (req, res) => {
const { email, password, first, last } = req.body;
let hashedPassword;
try {
bcrypt.hash(password, 12).then(function(hashed) {
hashedPassword = hashed;
db.Users.findOrCreate({
where: {
email: email,
password: hashedPassword,
first: first,
last: last
}
}).then(([user, created]) => {
if (!created) {
res.send('Something went wrong');
res.end();
} else {
let token;
try {
token = jwt.sign(
{ userId: user.id, email: user.email },
"I'm making curry chicken pitas for lunch",
{ expiresIn: '1hr' }
);
} catch (err) {
const error = new HttpError('Signin failed. Please try again', 500);
return next(error);
}
res
.status(200)
.json({ userId: user.id, email: user.email, token: token });
}
});
});
} catch (err) {
const error = new HttpError('Could not create user. Please try again', 500);
return next(error);
}
});
// LOG IN route for users to log in
app.get('/login', (req, res) => {
const { email, password } = req.query;
console.log("here's your request: ", email, password);
db.Users.findOne({
where: { email: email }
}).then(user => {
let isValidPassword = false;
try {
bcrypt.compare(password, user.password).then(function(bRes) {
isValidPassword = bRes;
if (!isValidPassword) {
console.log('not the right credentials, bro');
res
.status(401)
.json({
error:
'Could not log you in. Please check your credentials and try again'
});
} else {
let token;
try {
token = jwt.sign(
{ user: user.id, email: user.email },
"I'm making curry chicken pitas for lunch",
{ expiresIn: '1hr' }
);
} catch (err) {
const error = new HttpError(
'Server error while logging in. Please try again',
500
);
return next(error);
}
res
.status(200)
.json({ userId: user.id, email: user.email, token: token });
}
});
} catch (err) {
const error = new HttpError(
'Could not log you in. Please check your credentials and try again',
500
);
return next(error);
}
});
});
// check for token to continue to following routes
app.use(checkAuth);
// LOG OUT route for users to log in
app.get('/logout', (req, res) => {
db.Users.findOne({
where: { email: email }
}).then(user => {
res.send({ user });
});
});
// ---------- RECIPE ROUTES ----------//
// GET RECIPES route for recipebox
app.get('/recipebox', (req, res) => {
db.Recipe.findAll().then(recipes => {
res.send({ recipes });
});
});
//should submit-recipe to database//
app.post('/submit-recipe', (req, res) => {
console.log('storing a recipe...');
console.log(req.body);
let img = '/images/fooddefault.jpeg';
if (req.body.img) {
img = req.body.img;
}
db.Recipe.findOrCreate({
where: {
title: req.body.title,
description: req.body.description,
category: req.body.category,
img: img,
yield: req.body.yield,
time: req.body.time,
ingredients: req.body.ingredients,
instructions: req.body.instructions
}
}).then(([recipe, created]) => {
if (!created) {
res.send('Something went wrong');
res.end();
}
});
});