updated 404, added profile picture, templating, fixed registration bugs

This commit is contained in:
mrjvs
2018-12-18 18:21:54 +01:00
parent aa1f5e5157
commit c15284dbea
9 changed files with 54 additions and 25 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View File

@@ -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

View File

@@ -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('<I', pid);
const buff2 = Buffer.from(password).toString('ascii');

View File

@@ -47,8 +47,8 @@ module.exports = (app) => {
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'});

View File

@@ -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;

View File

@@ -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);

View File

@@ -1,14 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{> head }}
<body>
{{> navbar }}
<h1>haha-yes</h1>
file not found!
{{> footer }}
{{> head-common }}
<!-- page specific -->
<link rel="stylesheet" href="/assets/css/pretendo-contact.css">
</head>
<body class="flex">
<div class="contentWrapper bgLinesSmall">
<div class="bgExtrude"></div>
<section class="bigCardWrapper">
<div class="bigCard">
<p class="txt-highlight">ERROR</p>
<h1 class="txt-title">{{ locale.404.title }}</h1>
</div>
</section>
</div>
{{> footer-default }}
</body>
</html>
</html>

View File

@@ -1,7 +1,7 @@
<nav class="userNav">
<div class="userNavProfile">
<img src="" alt="">
<p class="name">Mrjvs</p>
<img src="{{ user.user.pnid.avatar_url }}" alt="{{ user.user.pnid.username }}">
<p class="name">{{ user.user.pnid.username }}</p>
</div>
<div class="links">
<a class="navItem active" href="#">ERROR</a>

View File

@@ -6,8 +6,8 @@
<div class="navSpread"></div>
{{#if user.isLoggedIn }}
<a class="navItem navProfile" href="/pnid/dashboard">
<img src="#" alt="">
<p>mrjvs@gmail.com</p>
<img src="{{ user.user.pnid.avatar_url }}" alt="{{ user.user.pnid.username }}">
<p>{{ user.user.pnid.username }}</p>
</a>
<a class="navItem navBtn" href="/pnid/logout">logout</a>
{{else}}