2017-10-11 10:15:19 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2017-10-12 17:25:34 -04:00
|
|
|
var async = require('async');
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2017-10-11 12:55:46 -04:00
|
|
|
var User = require('../models/User.js');
|
|
|
|
var Invite = require('../models/Invite.js');
|
2017-10-11 10:15:19 -04:00
|
|
|
|
|
|
|
var passport = require('passport');
|
|
|
|
|
2017-10-14 15:15:27 -04:00
|
|
|
var striptags = require('striptags');
|
|
|
|
|
2017-10-12 12:50:02 -04:00
|
|
|
function checkUsername(username, callback) {
|
2017-10-14 15:15:27 -04:00
|
|
|
if (username.length > 30) return callback(null, false);
|
|
|
|
if (striptags(username) !== username) return callback(null, false);
|
|
|
|
|
2017-10-11 12:55:46 -04:00
|
|
|
User.find({username: username}).limit(1).count(function(err, count) {
|
|
|
|
if (err) return callback(err);
|
2017-10-14 15:15:27 -04:00
|
|
|
(count === 0) ? callback(null, true) : callback(null, false);
|
2017-10-11 12:55:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-12 12:50:02 -04:00
|
|
|
function checkInvite(code, callback) {
|
2017-10-11 12:55:46 -04:00
|
|
|
Invite.findOne({code: code}, function(err, invite) {
|
|
|
|
if (err) return callback(err);
|
2017-10-12 12:50:02 -04:00
|
|
|
if (!invite || invite.used || invite.exp < new Date())
|
|
|
|
callback(null, false);
|
|
|
|
else
|
2017-10-11 12:55:46 -04:00
|
|
|
callback(null, true, invite);
|
2017-10-12 12:50:02 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function useInvite(code, username) {
|
|
|
|
Invite.updateOne({code: code}, {recipient: username, used: new Date()}, function(err, res) {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
2017-10-11 12:55:46 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 10:15:19 -04:00
|
|
|
router.post('/register', function(req, res) {
|
2017-10-12 12:50:02 -04:00
|
|
|
// Validate the parameters
|
|
|
|
async.parallel({
|
2017-10-13 16:16:26 -04:00
|
|
|
userCheck: function(callback) {
|
2017-10-12 17:25:34 -04:00
|
|
|
checkUsername(req.body.username, function(err, valid) {
|
2017-10-12 12:50:02 -04:00
|
|
|
callback(err, valid);
|
|
|
|
});
|
|
|
|
},
|
2017-10-13 16:16:26 -04:00
|
|
|
inviteCheck: function(callback) {
|
2017-10-12 17:25:34 -04:00
|
|
|
checkInvite(req.body.invite, function(err, valid, invite) {
|
2017-10-12 12:50:02 -04:00
|
|
|
callback(err, {valid: valid, invite: invite});
|
|
|
|
});
|
2017-10-11 12:55:46 -04:00
|
|
|
}
|
2017-10-12 17:25:34 -04:00
|
|
|
}, function(err, result) {
|
2017-10-13 16:16:26 -04:00
|
|
|
if (!result.userCheck) {
|
2017-10-14 15:15:27 -04:00
|
|
|
res.status(401).json({'message': 'Invalid username.'});
|
2017-10-13 16:16:26 -04:00
|
|
|
} else if (!result.inviteCheck.valid) {
|
2017-10-12 12:50:02 -04:00
|
|
|
res.status(401).json({'message': 'Invalid invite code.'});
|
|
|
|
} else {
|
2017-10-12 17:25:34 -04:00
|
|
|
useInvite(req.body.invite, req.body.username);
|
2017-10-11 12:55:46 -04:00
|
|
|
var user = new User();
|
|
|
|
user.username = req.body.username;
|
2017-10-13 16:16:26 -04:00
|
|
|
user.scope = result.inviteCheck.invite.scope;
|
2017-10-12 12:50:02 -04:00
|
|
|
user.date = new Date();
|
2017-10-11 12:55:46 -04:00
|
|
|
user.setPassword(req.body.password);
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2017-10-11 12:55:46 -04:00
|
|
|
user.save(function(err) {
|
2017-10-12 12:50:02 -04:00
|
|
|
if (err)
|
|
|
|
res.status(500).json({'message': 'Internal server error.'});
|
|
|
|
else
|
|
|
|
res.status(200).json({'token': user.genJwt()});
|
2017-10-11 12:55:46 -04:00
|
|
|
})
|
2017-10-12 12:50:02 -04:00
|
|
|
}
|
2017-10-11 12:55:46 -04:00
|
|
|
});
|
2017-10-11 10:15:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
router.post('/login', function(req, res) {
|
|
|
|
passport.authenticate('local', function(err, user, info) {
|
2017-10-12 12:50:02 -04:00
|
|
|
if (err)
|
|
|
|
res.status(500).json(err);
|
|
|
|
else if (user)
|
|
|
|
res.status(200).json({'token': user.genJwt() });
|
|
|
|
else
|
2017-10-11 10:15:19 -04:00
|
|
|
res.status(401).json(info);
|
2017-10-12 12:50:02 -04:00
|
|
|
|
2017-10-11 10:15:19 -04:00
|
|
|
})(req, res);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|