add service to generate guest account on server
This commit is contained in:
parent
1e6050a486
commit
ff3d6c2c24
2 changed files with 28 additions and 1 deletions
|
@ -3,6 +3,7 @@ const { validationResult } = require("express-validator");
|
||||||
const userQueries = require("../data/queries/user");
|
const userQueries = require("../data/queries/user");
|
||||||
const { hashPassword, compareHash } = require("../services/bcrypt");
|
const { hashPassword, compareHash } = require("../services/bcrypt");
|
||||||
const signToken = require("../services/signToken");
|
const signToken = require("../services/signToken");
|
||||||
|
const guestServices = require("../services/guestServices");
|
||||||
|
|
||||||
const checkValidationErrors = (req, res) => {
|
const checkValidationErrors = (req, res) => {
|
||||||
const errors = validationResult(req);
|
const errors = validationResult(req);
|
||||||
|
@ -65,7 +66,7 @@ const login = async (req, res, next) => {
|
||||||
const guest = async (req, res, next) => {
|
const guest = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
// username generator returns `Guest-${num}`
|
// username generator returns `Guest-${num}`
|
||||||
const username = "guest";
|
const { username, password } = guestServices.generateGuest();
|
||||||
// generateGuestUser();
|
// generateGuestUser();
|
||||||
const email = null;
|
const email = null;
|
||||||
// id generator returns `
|
// id generator returns `
|
||||||
|
@ -74,6 +75,7 @@ const guest = async (req, res, next) => {
|
||||||
signToken(res, user);
|
signToken(res, user);
|
||||||
res.send(user);
|
res.send(user);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
res.status(500).send({ errors: e });
|
res.status(500).send({ errors: e });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
25
packages/server/services/guestServices.js
Normal file
25
packages/server/services/guestServices.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const generateRandomPassword = () => {
|
||||||
|
const minLength = 8,
|
||||||
|
maxLength = 16,
|
||||||
|
minUTF = 33,
|
||||||
|
maxUTF = 126;
|
||||||
|
const randomize = (min, max) => Math.floor(Math.random() * (max - min) + min);
|
||||||
|
return Array(randomize(minLength, maxLength))
|
||||||
|
.fill(0)
|
||||||
|
.map(() => String.fromCharCode(randomize(minUTF, maxUTF)))
|
||||||
|
.join("");
|
||||||
|
};
|
||||||
|
|
||||||
|
const guestService = {
|
||||||
|
currentGuest: 0,
|
||||||
|
generateGuest() {
|
||||||
|
// generate unique username
|
||||||
|
const username = `Guest-${String(this.currentGuest++).padStart(6, 0)}`;
|
||||||
|
// generate random "password"
|
||||||
|
// this exists solely to add extra randomness to signed token and is not validated
|
||||||
|
const password = generateRandomPassword();
|
||||||
|
return { username, password };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = guestService;
|
Loading…
Reference in a new issue