From 09867e58b6848e34390950aecc6896dde2b6a09c Mon Sep 17 00:00:00 2001 From: hippoz Date: Thu, 3 Dec 2020 01:16:07 +0200 Subject: [PATCH] add color generation function and make more endpoints use the getpublicobject function --- api/v1/users.js | 28 +++++++--------------------- models/User.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/api/v1/users.js b/api/v1/users.js index 1274df7..26a1340 100755 --- a/api/v1/users.js +++ b/api/v1/users.js @@ -47,7 +47,6 @@ app.post('/account/create', [ const email = req.body.email; const startingRole = 'USER'; - const hashedPassword = await bcrypt.hash(unhashedPassword, config.bcryptRounds); const user = await User.create({ @@ -57,27 +56,20 @@ app.post('/account/create', [ role: startingRole }); - const responseUserObject = { - _id: user._id, - username: user.username, - email: user.email, - role: user.role, - permissionLevel: config.roleMap[user.role] - }; + const userObject = await user.getPublicObject(); - console.log('[*] [logger] [users] [create] User created', responseUserObject); + console.log('[*] [logger] [users] [create] User created', userObject); res.status(200).json({ error: false, message: 'SUCCESS_USER_CREATED', - user: responseUserObject + user: userObject }); } catch (e) { console.error('Internal server error', e); res.status(500).json({ error: true, message: 'INTERNAL_SERVER_ERROR' }); return; } - }); app.post('/token/create', [ @@ -112,7 +104,7 @@ app.post('/token/create', [ return; } - jwt.sign({ username }, secret.jwtPrivateKey, { expiresIn: '3h' }, (err, token) => { + jwt.sign({ username }, secret.jwtPrivateKey, { expiresIn: '3h' }, async (err, token) => { if (err) { res.status(500).json({ error: true, @@ -128,20 +120,14 @@ app.post('/token/create', [ }); } - const responseUserObject = { - _id: existingUser._id, - username: existingUser.username, - email: existingUser.email, - role: existingUser.role, - permissionLevel: config.roleMap[existingUser.role] - }; + const userObject = await existingUser.getPublicObject(); - console.log('[*] [logger] [users] [token create] Token created', responseUserObject); + console.log('[*] [logger] [users] [token create] Token created', userObject); res.status(200).json({ error: false, message: 'SUCCESS_TOKEN_CREATED', - user: responseUserObject, + user: userObject, token }); }); diff --git a/models/User.js b/models/User.js index cf93929..fe86fef 100755 --- a/models/User.js +++ b/models/User.js @@ -31,6 +31,35 @@ userSchema.method('getFullObject', function() { const User = mongoose.model('User', userSchema); +// NOTE(hippoz): These are all actually material design colors, taken from https://material-ui.com/customization/color/#playground +const colors = [ + '#f44336', + '#e91e63', + '#9c27b0', + '#673ab7', + '#3f51b5', + '#2196f3', + '#03a9f4', + '#00bcd4', + '#009688', + '#4caf50', + '#8bc34a', + '#cddc39', + '#ffeb3b', + '#ffc107', + '#ff9800', + '#ff5722' +]; + +User.generateColorFromUsername = function(username) { + let sum = 0; + for (let i in username) { + sum += username.charCodeAt(i); + } + const colorIndex = sum % colors.length; + return colors[colorIndex]; +}; + User.findByUsername = async function(username) { return await User.findOne({ username }).exec(); };