1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 00:46:38 -05:00

Add invites API route

This commit is contained in:
Jack Foltz 2018-01-14 09:52:40 -05:00
parent 6f707138f6
commit a00764316d
Signed by: foltik
GPG Key ID: 303F88F996E95541
2 changed files with 68 additions and 0 deletions

66
app/routes/invites.js Normal file
View File

@ -0,0 +1,66 @@
var express = require('express');
var router = express.Router();
var Invite = require('../models/Invite.js');
router.post('/create', function (req, res) {
if (!req.body.scope) {
res.status(400).json({'message': 'Bad request.'});
return;
}
var scope;
try {
scope = JSON.parse(req.body.scope);
} catch (e) {
res.status(500).json({'message': e.name + ': ' + e.message});
return;
}
var expiry = req.body.exp;
if (!expiry || expiry < Date.now())
expiry = 0;
var entry = {
code: crypto.randomBytes(12).toString('hex'),
scope: scope,
issuer: req.session.passport.user,
issued: Date.now(),
exp: expiry
};
Invite.create(entry, function (err) {
if (err) {
throw err;
} else {
res.status(200).json({
code: entry.code,
scope: entry.scope
});
}
})
});
router.get('/get', function (req, res, next) {
var query = {issuer: req.session.passport.user};
if (req.body.code)
query.code = req.body.code;
Invite.find(query, function (err, invites) {
if (err) {
next(err);
} else {
res.status(200).json(invites);
}
})
});
router.post('/delete', function (req, res, next) {
Invite.deleteOne({code: req.body.code}, function (err) {
if (err) next(err);
else res.status(200).json({'message': 'Successfully deleted.'});
});
});
module.exports = router;

View File

@ -7,6 +7,7 @@ var register = require('./register.js');
var login = require('./login.js');
var panel = require('./panel.js');
var keys = require('./keys.js');
var invites = require('./invites.js')
var Key = require('../models/Key.js');
@ -48,6 +49,7 @@ module.exports = function (app) {
app.use('/api/upload', upload);
app.use('/api/auth', auth);
app.use('/api/keys', requireLogin, keys);
app.use('/api/invites', requireLogin, invites);
app.use('/register', register);
app.use('/login', login);
app.use('/panel', requireLogin, panel);