-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
164 lines (135 loc) · 4.72 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
157
158
159
160
161
162
163
164
const express = require('express');
const bodyParser = require('body-parser');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const app = express();
const bcrypt = require('bcrypt');
const sequelize = require('./models').sequelize;
const PORT = process.env.PORT || 3333;
const dbUserOps = require('./DB_Sequelize/Users');
const dbExerciseOps = require('./DB_Sequelize/Exercises');
const dbWorkoutOps = require('./DB_Sequelize/Workouts');
const dbSetOps = require('./DB_Sequelize/Sets');
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('./public/dist'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
sequelize.sync().then(() => {
app.listen(PORT, () => console.log(`listening on port ${PORT}`));
});
passport.serializeUser((user, done) => {
done(null, user.Username);
});
passport.deserializeUser((username, done) => {
dbUserOps.findUserByUsername(username);
});
passport.use(new LocalStrategy((username, password, done) => {
dbUserOps.findUserByUsername(username).then(user => {
if (!user[0]) {
return done(null, false, {message: 'Incorrect Username'} );
} else {
bcrypt.compare(password, user[0].dataValues.Password, (err, res) => {
if (res) {
return done(null, user[0].dataValues);
} else {
return done(null, false, {message: 'Incorrect Password'});
}
})
}
});
}));
/////////// Register API ///////////////////
app.post('/register', (req, res) => {
// res.send("TEST Register end point");
dbUserOps.createNewUser(req.body)
.then(() => {
console.log('TEST', req.body.username);
dbUserOps.findUserByUsername(req.body.username)
.then(result => {
console.log('result of find', result);
res.send(result)
});
});
});
/////////// Login API ///////////////////
app.post('/login',
passport.authenticate('local'),
(req, res) => res.send(res.req.user)
);
app.get('/error', (req, res) => {
// res.set('Content-Type', 'application/json');
return res.send(req.authInfo);
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
//////////// Exercise Data API /////////////////////////
app.get('/exercise', (req, res) => {
dbExerciseOps.getAllExercises()
.then(result => res.send(result));
});
app.get('/exercise/:id', (req, res) => {
dbExerciseOps.getExerciseById(req.params.id)
.then(result => res.send(result));
});
app.post('/exercise', (req, res) => {
dbExerciseOps.createNewExercise(req.body)
.then((createdExercise) => res.send(createdExercise));
});
app.put('/exercise/:id', (req, res) => {
dbExerciseOps.updateExerciseById(req.params.id, req.body)
.then((updatedExercise) => res.send(updatedExercise));
});
app.delete('/exercise/:id', (req, res) => {
dbExerciseOps.deleteExerciseById(req.params.id)
.then(() => res.send('Exercise Deleted'));
});
///////// Workout Data API ////////////////////////////
app.get('/workout', (req, res) => {
dbWorkoutOps.getAllWorkouts()
.then(result => res.send(result));
});
app.get('/workout/:id', (req, res) => {
dbWorkoutOps.getWorkoutById(req.params.id)
.then(result => res.send(result));
});
app.post('/workout', (req, res) => {
dbWorkoutOps.createNewWorkout(req.body)
.then((createdWorkout) => res.send(createdWorkout));
});
app.put('/workout/:id', (req, res) => {
dbWorkoutOps.updateWorkoutById(req.params.id, req.body)
.then((updatedWorkout) => res.send(updatedWorkout));
});
app.delete('/workout/:id', (req, res) => {
dbWorkoutOps.deleteWorkoutById(req.params.id)
.then(() => res.send('Workout Deleted'));
});
///////// Set Data API ////////////////////////////
app.get('/set', (req, res) => {
dbSetOps.getAllSets()
.then(result => res.send(result));
});
app.get('/set/:id', (req, res) => {
dbSetOps.getSetById(req.params.id)
.then(result => res.send(result));
});
app.post('/set', (req, res) => {
dbSetOps.createNewSet(req.body)
.then((createdSet) => res.send(createdSet));
});
app.put('/set/:id', (req, res) => {
dbSetOps.updateSetById(req.params.id, req.body)
.then((updatedSet) => res.send(updatedSet));
});
app.delete('/set/:id', (req, res) => {
dbSetOps.deleteSetById(req.params.id)
.then(() => res.send('Set Deleted'));
});