-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchThisList.txt
95 lines (95 loc) · 2.7 KB
/
fetchThisList.txt
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
fetchThisSummary
A logged in user can retrieve with visible confirmation without causing a refresh/redirect.
GET /api/summary/:id
/app/:id
This page displays summary of associated tasks, as well as a navigation bar with login/signup or logout buttons.
If logged in user owns the task, page displays buttons update and delete.
GET /app/:id
POST /app/:id
DELETE /app/:id
_______________________________________________________
Persist the User State
-[x] create session secret in .env, and Config file
```
module.exports = {
environment: process.env.NODE_ENV || 'development',
port: process.env.PORT || 8080,
sessionSecret: process.env.SESSION_SECRET,
```
-[x] in app.js import const session = require('express-session')
const app = express();
const { sessionSecret } = require('./config');
-[x] in app.js user cookie parser to pass the session secret
app.use(cookieParser(sessionSecret));
const summaryRouter = require('./routes/summary');
-[x] create & export in utils.pug /checkUserAvailable
-[x] import checkUserAvailable
-[x] update summary.js
```
const express = require("express");
const db = require('../db/models');
const { Task } = db;
const { csrfProtection, asyncHandler,checkUserAvailable } = require('../utils');
const router = express.Router();
```
-[x] touch routes/summary.js
```
router.get(
"/", checkUserAvailable,
asyncHandler(async (req, res, next) => {
const tasks = await Task.findAll();
if (tasks) {
res.json({ tasks });
} else {
next(taskNotFoundError(taskId));
}
})
);
router.get(
"/:id(\\d+)",
checkUserAvailable,
asyncHandler(async (req, res, next) => {
const taskId = parseInt(req.params.id, 10);
const task = await Task.findByPk(taskId);
if (task) {
res.json({ task });
} else {
next(taskNotFoundError(taskId));
}
})
);
const taskNotFoundError = (id) => {
const err = Error(`Task with id of ${id} could not be found.`);
err.title = "Task not found.";
err.status = 404;
return err;
};
router.patch(
"/:id(\\d+)", checkUserAvailable,
asyncHandler(async (req, res, next) => {
const taskId = parseInt(req.params.id, 10);
const task = await Task.findByPk(taskId);
if (task) {
await task.update({ name: req.body.name });
res.json({ task });
} else {
next(taskNotFoundError(taskId));
}
})
);
router.delete(
"/:id(\\d+)",checkUserAvailable,
asyncHandler(async (req, res, next) => {
const taskId = parseInt(req.params.id, 10);
const task = await Task.findByPk(taskId);
if (task) {
await task.destroy();
res.status(204).end();
} else {
next(taskNotFoundError(taskId));
}
})
);
module.exports = router;
```
-[] touch views/summary.pug