2018-05-02 09:32:43 +00:00
|
|
|
const express = require('express');
|
2018-03-22 15:45:11 +00:00
|
|
|
const asyncHandler = require('express-async-handler');
|
2018-03-21 15:51:46 +00:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
|
|
|
|
|
2018-03-22 15:45:11 +00:00
|
|
|
module.exports = (repository) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
2018-03-22 15:45:11 +00:00
|
|
|
var router = express.Router();
|
|
|
|
|
|
|
|
router.post('/upload', asyncHandler(async (req, res) =>
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
if (!req.body.code)
|
|
|
|
{
|
|
|
|
res.sendStatus(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:07:09 +00:00
|
|
|
var code = await repository.codes.get(req.body.code);
|
|
|
|
if (code !== null)
|
2018-03-21 15:51:46 +00:00
|
|
|
{
|
|
|
|
jwt.sign({
|
2018-05-13 07:33:10 +00:00
|
|
|
codeId: req.body.code,
|
2018-08-16 13:18:02 +00:00
|
|
|
codeUserId: code.userId
|
2018-03-22 15:45:11 +00:00
|
|
|
}, config.jwtSecret, (err, token) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
res.sendStatus(500);
|
|
|
|
else
|
|
|
|
res.send(token);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res.sendStatus(403);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
router.post('/login', asyncHandler(async (req, res) =>
|
|
|
|
{
|
|
|
|
if (!req.body.username || !req.body.password)
|
|
|
|
{
|
|
|
|
res.sendStatus(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = await repository.users.getLoginUser(req.body.username, req.body.password);
|
|
|
|
if (user !== null)
|
|
|
|
{
|
|
|
|
jwt.sign({
|
2018-04-26 13:39:21 +00:00
|
|
|
userId: user.id
|
2018-03-21 15:51:46 +00:00
|
|
|
}, config.jwtSecret, (err, token) =>
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
res.sendStatus(500);
|
|
|
|
else
|
|
|
|
res.send(token);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res.sendStatus(403);
|
2018-03-22 15:45:11 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
return router;
|
2018-03-21 15:51:46 +00:00
|
|
|
}
|