Implemented username reserve feature

This commit is contained in:
2021-03-03 04:24:33 +11:00
parent 3d4bae1281
commit e3ef30c10c
7 changed files with 101 additions and 6 deletions
+2 -5
View File
@@ -1,10 +1,7 @@
const express = require('express');
const router = express.Router();
//the routes
//TODO: import the routes here
//basic route management
//TODO: define the routes here
//reserve the name using a pseudonym
router.post('/reserve', require('./reserve'));
module.exports = router;
+34
View File
@@ -0,0 +1,34 @@
const crypto = require("crypto");
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
const { pseudonyms } = require('../database/models');
const route = async (req, res) => {
//generate a UUID to act as a pseudonym (starting with a period)
const pseudonym = `.${uuid()}`;
//find or create the record (using the username)
const [instance, created] = await pseudonyms.findOrCreate({
where: {
username: req.fields.username
}
});
//save the pseudonym (overwriting existing pseudonyms)
const result = await pseudonyms.update({
pseudonym: pseudonym
},{
where: {
username: req.fields.username
}
});
//OK
return res.status(200).send({ pseudonym: pseudonym });
};
//lazy
const uuid = (bytes = 16) => crypto.randomBytes(bytes).toString("hex");
module.exports = route;