diff --git a/db/db.js b/db/db.js index f70865efcbaeae828ef801239e00d6a647e95253..16e92302884b24d9c8b633355105e457e78a4eba 100644 --- a/db/db.js +++ b/db/db.js @@ -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 diff --git a/db/reset_db.js b/db/reset_db.js new file mode 100644 index 0000000000000000000000000000000000000000..0b437787d245622bbacbfc995611496770cd6cb0 --- /dev/null +++ b/db/reset_db.js @@ -0,0 +1,21 @@ +//------------------------------------------------- +// 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 diff --git a/index.js b/index.js index e3257f09968cfc02addd495aff1b2b205c62b9b3..f601fecc9729cd87959c06dcb686b367d9025e54 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/middleware/browseMW.js b/middleware/browseMW.js index 59f04472ce0c4c04e56163746b01e264b13cf6d7..fe51faababdf0d575a48be8215e70e76faefe44e 100644 --- a/middleware/browseMW.js +++ b/middleware/browseMW.js @@ -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 diff --git a/middleware/profileeditMW.js b/middleware/profileeditMW.js index 1eba770fc8e738c715ecd67e52ba601bd20f7cd1..83404d47e52a625c00f6413fe66aae2624c86f3e 100644 --- a/middleware/profileeditMW.js +++ b/middleware/profileeditMW.js @@ -1,7 +1,41 @@ +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 diff --git a/middleware/profileloadMW.js b/middleware/profileloadMW.js index 3e8bb8a2480037de89c522a50277689c5f8043e8..ed921c69380c7719b0b08cd4e57ab7147fab0dc6 100644 --- a/middleware/profileloadMW.js +++ b/middleware/profileloadMW.js @@ -1,16 +1,17 @@ -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 diff --git a/middleware/registerMW.js b/middleware/registerMW.js index 1bce17145671eaf050819140cad774df66e7d461..161335b94485c98b5488843f8f3a6b63b2e474aa 100644 --- a/middleware/registerMW.js +++ b/middleware/registerMW.js @@ -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'); diff --git a/middleware/selectMW.js b/middleware/selectMW.js index 9a7f53dbd11d04b3f6e3804b309a14e4a9fc9242..e5552027972b842350cfbfeae43d3ca54582fcd3 100644 --- a/middleware/selectMW.js +++ b/middleware/selectMW.js @@ -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 diff --git a/views/_head.ejs b/views/_head.ejs index 5e81a4373e32c73cbd395520417bff07831999f8..0369aee802aabd08d85b82085b3243d89e25cb9f 100644 --- a/views/_head.ejs +++ b/views/_head.ejs @@ -12,8 +12,14 @@ <header> <a href="/">SzobaTinder.sch</a> <span> - <a href="/matches">Our matches</a> - <a href="/profile">Our profile</a> + <% 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> diff --git a/views/profile.ejs b/views/profile.ejs index 6a12736a474b5af8366840068b2715db8d1111cf..6dc820cec1d2a032d64a50f757ac5d50512e3548 100644 --- a/views/profile.ejs +++ b/views/profile.ejs @@ -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