1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-30 14:31:42 -05:00

Add invite codes for registering

This commit is contained in:
Jack 2017-10-11 12:55:46 -04:00
parent 8251900309
commit e6a09bbe40
Signed by: foltik
GPG Key ID: 303F88F996E95541
6 changed files with 80 additions and 24 deletions

17
app/models/Invite.js Normal file
View File

@ -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);

View File

@ -18,7 +18,8 @@ var UserSchema = mongoose.Schema({
},
level: Number,
hash: String,
salt: String
salt: String,
date: Date
});
UserSchema.methods.setPassword = function(password) {

View File

@ -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);
}

View File

@ -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');
});

View File

@ -7,5 +7,9 @@
<label for="password">Password</label>
<input id="password" class="form-control" type="password" ng-model="password"/>
</div>
<div class="form-group">
<label for="invite">Invite Code</label>
<input id="invite" class="form-control" type="text" ng-model="invite"/>
</div>
<button type="submit" class="btn">Register</button>
</form>

View File

@ -16,7 +16,7 @@
<script src="/js/app.js"></script>
</head>
<body ng-app="shimapan">
<div id="container" ng-controller="RegisterController">
<div id="container">
<register-component></register-component>
</div>
</body>