From e6a09bbe40ff7d0085480a19cd2f7953f7655f5d Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 11 Oct 2017 12:55:46 -0400 Subject: [PATCH] Add invite codes for registering --- app/models/Invite.js | 17 ++++++++ app/models/User.js | 3 +- app/routes/auth.js | 75 ++++++++++++++++++++++++++---------- public/js/components/RegisterComp.js | 3 +- public/views/register-form.html | 4 ++ public/views/register.html | 2 +- 6 files changed, 80 insertions(+), 24 deletions(-) create mode 100644 app/models/Invite.js diff --git a/app/models/Invite.js b/app/models/Invite.js new file mode 100644 index 0000000..cb6e126 --- /dev/null +++ b/app/models/Invite.js @@ -0,0 +1,17 @@ +var mongoose = require('mongoose'); + +var InviteSchema = mongoose.Schema({ + code: { + type: String, + unique: true, + required: true + }, + level: Number, + issuer: String, + recipient: String, + issued: Date, + used: Date, + exp: Date +}); + +module.exports = mongoose.model('Invite', InviteSchema); \ No newline at end of file diff --git a/app/models/User.js b/app/models/User.js index 502fad7..9378109 100644 --- a/app/models/User.js +++ b/app/models/User.js @@ -18,7 +18,8 @@ var UserSchema = mongoose.Schema({ }, level: Number, hash: String, - salt: String + salt: String, + date: Date }); UserSchema.methods.setPassword = function(password) { diff --git a/app/routes/auth.js b/app/routes/auth.js index 7455d03..083aaf7 100644 --- a/app/routes/auth.js +++ b/app/routes/auth.js @@ -5,28 +5,64 @@ var express = require('express'); var router = express.Router(); var mongoose = require('mongoose'); -var User = mongoose.model('User'); +var User = require('../models/User.js'); +var Invite = require('../models/Invite.js'); var passport = require('passport'); -router.post('/register', function(req, res) { - console.log(req.body); - - var user = new User(); - user.username = req.body.username; - user.level = 0; - user.genApiKey(); - user.setPassword(req.body.password); - - user.save(function(err) { - var token; - token = user.genJwt(); - res.status(200); - res.json({ - "token": token - }); +function validUsername(username, callback) { + User.find({username: username}).limit(1).count(function(err, count) { + if (err) return callback(err); + count === 0 ? callback(null, true) : callback(null, false); }); +} +function useInvite(code, user, callback) { + Invite.findOne({code: code}, function(err, invite) { + if (err) return callback(err); + if (!invite || invite.used) { + return callback(null, false, null); + } else { + Invite.updateOne({code: code}, {recipient: user, used: Date.now()}, function(err, res) { + if (err) throw err; + }); + callback(null, true, invite); + } + }) +} + +router.post('/register', function(req, res) { + // Check the username + validUsername(req.body.username, function(err, valid) { + if (!valid) { + res.status(401).json({'message': 'Username in use.'}); + return; + } + + // Check and use the invite + useInvite(req.body.invite, req.body.username, function(err, valid, invite) { + if (!valid) { + res.status(401).json({'message': 'Invalid invite code.'}); + return; + } + + var user = new User(); + user.username = req.body.username; + user.level = invite.level; + user.date = Date.now(); + user.genApiKey(); + user.setPassword(req.body.password); + + user.save(function(err) { + if (err) { + res.status(500).json({'message': 'Internal server error'}); + } else { + var token = user.genJwt(); + res.status(200).json({'token': token}); + } + }) + }) + }); }); router.post('/login', function(req, res) { @@ -39,10 +75,7 @@ router.post('/login', function(req, res) { var token; if (user) { token = user.genJwt(); - res.status(200); - res.json({ - "token": token - }); + res.status(200).json({'token': token }); } else { res.status(401).json(info); } diff --git a/public/js/components/RegisterComp.js b/public/js/components/RegisterComp.js index ffcc2eb..77e7e05 100644 --- a/public/js/components/RegisterComp.js +++ b/public/js/components/RegisterComp.js @@ -2,7 +2,8 @@ function RegisterController($scope, AuthService) { $scope.register = function() { AuthService.register({ username: $scope.username, - password: $scope.password + password: $scope.password, + invite: $scope.invite }).then(function() { alert('Registered'); }); diff --git a/public/views/register-form.html b/public/views/register-form.html index c15fa5e..556aa8c 100644 --- a/public/views/register-form.html +++ b/public/views/register-form.html @@ -7,5 +7,9 @@ +
+ + +
\ No newline at end of file diff --git a/public/views/register.html b/public/views/register.html index 6dcb530..fe8307f 100644 --- a/public/views/register.html +++ b/public/views/register.html @@ -16,7 +16,7 @@ -
+