Skip to content
Snippets Groups Projects
Commit 90ee5fb9 authored by Ferenc Schulcz's avatar Ferenc Schulcz
Browse files

Profile editing

parent aa01dbab
Branches
No related tags found
No related merge requests found
......@@ -7,11 +7,33 @@ mongoose.connect('mongodb://localhost/szobatinder');
const User = mongoose.model('user', {
username: String,
password: String
})
password: String,
name: String,
bio: String,
member1: String,
member2: String,
Member3: String,
contact: String,
active: Boolean
});
function emptyUser() {
return new User({
username: "",
password: "",
name: "",
bio: "",
member1: "",
member2: "",
Member3: "",
contact: "",
active: false
});
}
function createUser(username, password, callback) {
const user = new User({ username: username, password: password }); // using double ROT-13 for password encryption
const user = emptyUser();
user.username = username; user.password = password; // using double ROT-13 for password encryption
user.save(err => {
console.error(err);
callback(err);
......@@ -36,8 +58,36 @@ function checkUserCredentials(username, password) {
});
}
function getUser(username) {
return new Promise((resolve, reject) => {
User.findOne({ username: username }, (err, user) => {
if (user == null) reject('No such user');
else resolve(user);
});
})
}
function updateProfile(username, name, bio, member1, member2, member3, contact, active) {
return new Promise((resolve, reject) => {
User.updateOne({ username: username }, {
name: name,
bio: bio,
member1: member1,
member2: member2,
member3: member3,
contact: contact,
active: active,
}, undefined, (err, result) => {
if (typeof err == 'undefined' || err == null) resolve('ok');
else reject(err);
})
})
}
module.exports = {
createUser: createUser,
doesUserExist: doesUserExist,
checkUserCredentials: checkUserCredentials,
getUser: getUser,
updateProfile: updateProfile,
};
\ No newline at end of file
//-------------------------------------------------
// ONLY for development purposes!
//-------------------------------------------------
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/szobatinder');
const User = mongoose.model('user', {
username: String,
password: String,
name: String,
bio: String,
member1: String,
member2: String,
Member3: String,
contact: String,
active: Boolean
});
User.deleteMany({});
\ No newline at end of file
......@@ -43,7 +43,7 @@ app.get('/register', renderMW(objectrepository, 'register'));
app.post('/register', registerMW(objectrepository));
app.get('/logout', logoutMW(objectrepository));
app.get('/browse', authMW(), browseMW(objectrepository), profileloadMW(objectrepository), renderMW(objectrepository, 'browse'));
app.get('/browse', authMW(), browseMW(objectrepository), profileloadMW(), renderMW(objectrepository, 'browse'));
app.post('/browse/like', authMW(), selectMW(), profileloadMW(), likeMW());
app.get('/browse/match', authMW(), renderMW(objectrepository, 'newmatch'));
app.post('/browse/dislike', authMW(), selectMW(), profileloadMW(), dislikeMW());
......@@ -56,7 +56,7 @@ app.param('username', (req, res, next, username) => {
});
app.get('/user/:username', authMW(), profileloadMW(), renderMW(objectrepository, 'match'));
app.get('/profile', authMW(), profileloadMW(), renderMW(objectrepository, 'profile'));
app.get('/profile', authMW(), profileloadMW(true), renderMW(objectrepository, 'profile'));
app.post('/profile', authMW(), profileeditMW());
const server_port = 3000;
......
......@@ -2,7 +2,7 @@
module.exports = function (objectrepository) {
return function (req, res, next) {
// selects a username that the current user has no relation with yet, and places it on res.locals.userToLoad.
res.locals.userToLoad = "l33tb0yz";
res.locals.userToLoad = "test";
return next();
}
}
\ No newline at end of file
const db = require("../db/db");
module.exports = function (objectrepository) {
return function (req, res, next) {
// reads profile data from the requests and saves it for the current user.
return next();
const name = req.body.name;
const bio = req.body.bio;
const member1 = req.body.member1;
const member2 = req.body.member2;
const member3 = req.body.member3;
const contact = req.body.contact;
const active = req.body.active;
if (
typeof name == 'undefined' ||
typeof bio == 'undefined' ||
typeof member1 == 'undefined' ||
typeof member2 == 'undefined' ||
typeof member3 == 'undefined' ||
typeof contact == 'undefined' ||
typeof active == 'undefined'
) {
return res.status(400).render('profile');
}
if (
name == '' ||
bio == '' ||
member1 == '' ||
contact == '' ||
active == ''
) {
res.locals.error = 'Missing a required field.';
return res.status(400).render('profile');
}
db.updateProfile(res.locals.username, name, bio, member1, member2, member3, contact, active == "on")
.then(res.redirect('/profile'));
}
}
\ No newline at end of file
module.exports = function (objectrepository) {
const db = require('../db/db.js');
module.exports = function (ownProfile) {
return function (req, res, next) {
// loads a profile and places its data on res.locals.profile. It takes the username to load the profile for from res.locals.userToLoad.
res.locals.profile = {
name: 'Legjobbszoba',
bio: 'Folyton bulika, nappal alszunk, éjjel iszunk',
member1: 'Minta Géza',
member2: 'Alkoh Olivér',
member3: '',
contact: 'minta.geza@example.com',
active: true
};
return next();
// loads a profile and places its data on res.locals.profile. It takes the username to load the profile for from res.locals.userToLoad or from the current user.
userToLoad = res.locals.userToLoad;
if (ownProfile === true) userToLoad = res.locals.username;
return db.getUser(userToLoad).then(user => {
res.locals.profile = user;
next();
}).catch(err => {
console.error(err);
res.status(500).end();
})
}
}
\ No newline at end of file
......@@ -4,9 +4,9 @@ module.exports = function (objectrepository) {
return function (req, res, next) {
// takes a username, a password and a password-again from the registration form. If the username is not taken, and the passwords match, creates the user and creates a session and redirects to `/`. Otherwise it displays an error.
username = req.body.user;
password = req.body.pass;
password2 = req.body.pass2;
const username = req.body.user;
const password = req.body.pass;
const password2 = req.body.pass2;
if (typeof username == 'undefined' || typeof password == 'undefined' || typeof password2 == 'undefined') {
return res.status(400).render('register');
......
......@@ -2,7 +2,7 @@
module.exports = function (objectrepository) {
return function (req, res, next) {
// selects a username from a request parameter and places it on res.locals.userToLoad.
res.locals.userToLoad = "l33tb0yz";
res.locals.userToLoad = "test";
return next();
}
}
\ No newline at end of file
......@@ -12,8 +12,14 @@
<header>
<a href="/">SzobaTinder.sch</a>
<span>
<% if (typeof username=='undefined' ) { %>
<a href="/register">Register</a>
<a href="/login">Log in</a>
<% } else { %>
<a href="/matches">Our matches</a>
<a href="/profile">Our profile</a>
<a href="/logout">Log out</a>
<% } %>
</span>
</header>
......
......@@ -2,7 +2,7 @@
<h1>Our profile</h1>
<form action="/profile">
<form action="/profile" , method="post">
<label for="name">Name: </label>
<input type="text" id="name" name="name" value="<%= profile.name %>"><br>
<p class="form-helper">You surely have a colorful name!</p>
......@@ -29,10 +29,8 @@
<label for="active">Show in browsing: </label>
<input type="checkbox" id="active" name="active" checked="<%= profile.active %>"><br>
<p class="form-helper">If you untick this box, your profile will be hidden from potential matches</p>
</form>
<section id="actionbar">
<a class="button" href="/profile">Save</a>
</section>
<input type="submit" class="button" value="Save">
</form>
<%- include('_tail', {}) %>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment