-
Notifications
You must be signed in to change notification settings - Fork 11
/
setInfo.js
57 lines (50 loc) · 1.38 KB
/
setInfo.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
import { Router } from 'express';
import { jwtAuth } from '../../middleware/jwt';
import {
userCollection as dbRef,
} from '../../util/firebase';
const router = Router();
router.post('/email/:id', jwtAuth('write'), async (req, res, next) => {
try {
const { id } = req.params;
if (req.user.user !== id) {
res.status(401).send('LOGIN_NEEDED');
return;
}
const doc = await dbRef.doc(id).get();
if (!doc.exists) throw new Error('user not found');
const {
isEmailEnabled,
} = req.body;
if (typeof (isEmailEnabled) !== 'boolean') throw new Error('invalid input');
await doc.ref.update({ isEmailEnabled });
res.sendStatus(200);
} catch (err) {
next(err);
}
});
router.put('/read/:id', jwtAuth('write'), async (req, res, next) => {
try {
const { id } = req.params;
if (req.user.user !== id) {
res.status(401).send('LOGIN_NEEDED');
return;
}
const doc = await dbRef.doc(id).get();
if (!doc.exists) throw new Error('user not found');
const {
likebuttonIntro,
} = req.body;
const updateObj = {};
if (typeof (likebuttonIntro) !== 'boolean') {
throw new Error('invalid input');
} else {
updateObj['read.likebuttonIntro'] = likebuttonIntro;
}
await dbRef.doc(id).update(updateObj);
res.sendStatus(200);
} catch (err) {
next(err);
}
});
export default router;