add color generation function and make more endpoints use the getpublicobject function

This commit is contained in:
hippoz 2020-12-03 01:16:07 +02:00
parent bdfdd1a460
commit 09867e58b6
2 changed files with 36 additions and 21 deletions

View file

@ -47,7 +47,6 @@ app.post('/account/create', [
const email = req.body.email; const email = req.body.email;
const startingRole = 'USER'; const startingRole = 'USER';
const hashedPassword = await bcrypt.hash(unhashedPassword, config.bcryptRounds); const hashedPassword = await bcrypt.hash(unhashedPassword, config.bcryptRounds);
const user = await User.create({ const user = await User.create({
@ -57,27 +56,20 @@ app.post('/account/create', [
role: startingRole role: startingRole
}); });
const responseUserObject = { const userObject = await user.getPublicObject();
_id: user._id,
username: user.username,
email: user.email,
role: user.role,
permissionLevel: config.roleMap[user.role]
};
console.log('[*] [logger] [users] [create] User created', responseUserObject); console.log('[*] [logger] [users] [create] User created', userObject);
res.status(200).json({ res.status(200).json({
error: false, error: false,
message: 'SUCCESS_USER_CREATED', message: 'SUCCESS_USER_CREATED',
user: responseUserObject user: userObject
}); });
} catch (e) { } catch (e) {
console.error('Internal server error', e); console.error('Internal server error', e);
res.status(500).json({ error: true, message: 'INTERNAL_SERVER_ERROR' }); res.status(500).json({ error: true, message: 'INTERNAL_SERVER_ERROR' });
return; return;
} }
}); });
app.post('/token/create', [ app.post('/token/create', [
@ -112,7 +104,7 @@ app.post('/token/create', [
return; return;
} }
jwt.sign({ username }, secret.jwtPrivateKey, { expiresIn: '3h' }, (err, token) => { jwt.sign({ username }, secret.jwtPrivateKey, { expiresIn: '3h' }, async (err, token) => {
if (err) { if (err) {
res.status(500).json({ res.status(500).json({
error: true, error: true,
@ -128,20 +120,14 @@ app.post('/token/create', [
}); });
} }
const responseUserObject = { const userObject = await existingUser.getPublicObject();
_id: existingUser._id,
username: existingUser.username,
email: existingUser.email,
role: existingUser.role,
permissionLevel: config.roleMap[existingUser.role]
};
console.log('[*] [logger] [users] [token create] Token created', responseUserObject); console.log('[*] [logger] [users] [token create] Token created', userObject);
res.status(200).json({ res.status(200).json({
error: false, error: false,
message: 'SUCCESS_TOKEN_CREATED', message: 'SUCCESS_TOKEN_CREATED',
user: responseUserObject, user: userObject,
token token
}); });
}); });

View file

@ -31,6 +31,35 @@ userSchema.method('getFullObject', function() {
const User = mongoose.model('User', userSchema); 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) { User.findByUsername = async function(username) {
return await User.findOne({ username }).exec(); return await User.findOne({ username }).exec();
}; };