-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.js
More file actions
32 lines (29 loc) · 731 Bytes
/
auth.js
File metadata and controls
32 lines (29 loc) · 731 Bytes
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
const cfg = require('./config')
const jwt = require('jwt-simple')
const user = require('./model/users_model')
const {FSError} = require('./response-types')
// eslint-disable-next-line
module.exports.authenticate = async (req, res, next) => {
const token = req.get('token')
try {
const {id, uuid} = jwt.decode(token, cfg.jwtSecret)
try {
await user.authenticate(id, uuid)
req.user = {id, uuid}
next()
} catch (e) {
next(e)
}
} catch (e) {
console.error(e)
next(FSError.unauthorized())
}
}
module.exports.authorizedToDelete = async ({body: {post}, user: {id}}, res, next) => {
try {
await user.authorizedToDelete(post, id)
next()
} catch (e) {
next(e)
}
}