diff --git a/assets/images/default_avatar.png b/assets/images/default_avatar.png new file mode 100644 index 0000000..9b64915 Binary files /dev/null and b/assets/images/default_avatar.png differ diff --git a/helpers/util.js b/helpers/util.js index cf71125..3faccc2 100644 --- a/helpers/util.js +++ b/helpers/util.js @@ -10,13 +10,13 @@ const logger = require('winston'); // shows 404 template. takes express response object function send404(res) { - res.status(404).send('404'); + res.status(404).render('404'); } function templateReadyUser(req) { // normal user logged in const isLoggedIn = req.user ? req.user.role != 'admin' : false; - const user = req.user; + const user = req.user ? req.user.toObject() : undefined; return { isLoggedIn, user diff --git a/models/pnid.js b/models/pnid.js index 5fc2f94..4f161d8 100644 --- a/models/pnid.js +++ b/models/pnid.js @@ -19,6 +19,7 @@ const PNIDSchema = new mongoose.Schema({ required: [true, 'Email is required.'], unique: true, trim: true, + uppercase: true, validate: [validateEmail, 'Please fill a valid email address'] }, email_validated: { @@ -48,11 +49,17 @@ const PNIDSchema = new mongoose.Schema({ }, username: { type: String, - unique: true + unique: true, + validate: [validateUsername, 'Please fill a valid username'], + required: true }, username_lower: { type: String, unique: true + }, + avatar_url: { + type: String, + default: '/assets/images/default_avatar.png' } }, consoles: [] @@ -64,6 +71,12 @@ function validateEmail(email) { return re.test(email); } +function validateUsername(username) { + // eslint throws "unnecesary character escape" + const re = /^[a-zA-Z0-9]+([_ -]?[a-zA-Z0-9])*$/; // eslint-disable-line + return re.test(username); +} + PNIDSchema.plugin(uniqueValidator, {message: '{PATH} already in use.'}); // hashing password @@ -86,10 +99,22 @@ PNIDSchema.pre('save', function(next) { PNIDSchema.statics.findByEmail = function(email) { return this.model('pnid').findOne({ - email + email: email.toUpperCase() }); }; +PNIDSchema.statics.findUser = async function(key) { + const user = await this.model('pnid').findOne({ + email: key.toUpperCase() + }); + if (!user) { + return await this.model('pnid').findOne({ + 'pnid.username_lower': key.toLowerCase() + }); + } + return user; +}; + PNIDSchema.statics.hashPasswordPrimary = function(password, pid) { const buff1 = require('python-struct').pack(' { passport.use('PNIDStrategy', new LocalStrategy({ usernameField: 'email' }, (email, password, done) => { - // find user in database - PNIDModel.findByEmail(email).then((user) => { + // find user in database, email = username or email + PNIDModel.findUser(email).then((user) => { if (!user) { // user doesnt exist return done(null, false, {message: 'Incorrect email'}); diff --git a/routes/pnid.js b/routes/pnid.js index a255c71..86d3127 100644 --- a/routes/pnid.js +++ b/routes/pnid.js @@ -92,15 +92,15 @@ router.post('/api/v1/login', passport.authenticate('PNIDStrategy'), function (re * errors: Strings[messages] * } */ -router.post('/api/v1/register', recaptcha.middleware.verify, async (request, response) => { +router.post('/api/v1/register'/*, recaptcha.middleware.verify*/, async (request, response) => { if (!request.body) { return apiHelper.sendApiGenericError(response); } - if (request.recaptcha.error) { + /*if (request.recaptcha.error) { logger.log('warn', `[reCaptcha ERROR] ${request.recaptcha.error} | IP: ${request.ip} | Data: ${JSON.stringify(request.body)}`); return apiHelper.sendApiError(response, 500, ['Captcha error']); - } + }*/ const { email, password, confirm_password, username } = request.body; diff --git a/server.js b/server.js index cacd7b3..0aff1ac 100644 --- a/server.js +++ b/server.js @@ -95,8 +95,6 @@ app.use((request, response) => { return utilHelper.send404(response); }); -// TODO improve error handling -// TODO remove param decoding errors from logs example: "host/test/%" // 4 parameters required to read the error, cant help the eslint error app.use((error, request, response, next) => { // eslint-disable-line no-unused-vars logger.log('warn', error.stack); diff --git a/views/404.hbs b/views/404.hbs index faa5dad..e2ba727 100644 --- a/views/404.hbs +++ b/views/404.hbs @@ -1,14 +1,20 @@ - {{> head }} - - {{> navbar }} - -

haha-yes

- - file not found! - - {{> footer }} + {{> head-common }} + + + + +
+
+
+
+

ERROR

+

{{ locale.404.title }}

+
+
+
+ {{> footer-default }} - + \ No newline at end of file diff --git a/views/partials/nav-dashboard.hbs b/views/partials/nav-dashboard.hbs index 1ff4e15..a6240c7 100644 --- a/views/partials/nav-dashboard.hbs +++ b/views/partials/nav-dashboard.hbs @@ -1,7 +1,7 @@