add color generation function and make more endpoints use the getpublicobject function
This commit is contained in:
parent
bdfdd1a460
commit
09867e58b6
2 changed files with 36 additions and 21 deletions
|
@ -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
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
};
|
||||
|
|
Reference in a new issue