From e5dc46846ce9fb1c990d291ef852c0cad3f74957 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 9 Jun 2014 11:26:04 -0400 Subject: editing profile --- public/assets/javascripts/app.js | 2 +- public/assets/javascripts/ui/EditProfileModal.js | 34 +++++++++- public/assets/javascripts/ui/MasterView.js | 77 ----------------------- public/assets/javascripts/ui/ModalFormView.js | 70 --------------------- public/assets/javascripts/ui/ModalView.js | 28 --------- public/assets/javascripts/ui/Router.js | 77 +++++++++++++++++++++++ public/assets/javascripts/ui/SignUpModal.js | 2 - public/assets/javascripts/vendor/ModalFormView.js | 76 ++++++++++++++++++++++ public/assets/javascripts/vendor/ModalView.js | 28 +++++++++ public/assets/stylesheets/app.css | 7 ++- server/index.js | 2 +- server/lib/api.js | 22 +++++-- server/lib/auth.js | 17 ++++- server/lib/util.js | 2 + server/lib/views.js | 5 +- views/partials/edit-profile.ejs | 29 ++++++--- views/partials/header.ejs | 2 +- views/partials/scripts.ejs | 7 ++- 18 files changed, 285 insertions(+), 202 deletions(-) delete mode 100644 public/assets/javascripts/ui/MasterView.js delete mode 100644 public/assets/javascripts/ui/ModalFormView.js delete mode 100644 public/assets/javascripts/ui/ModalView.js create mode 100644 public/assets/javascripts/ui/Router.js create mode 100644 public/assets/javascripts/vendor/ModalFormView.js create mode 100644 public/assets/javascripts/vendor/ModalView.js diff --git a/public/assets/javascripts/app.js b/public/assets/javascripts/app.js index b541d47..310dc69 100644 --- a/public/assets/javascripts/app.js +++ b/public/assets/javascripts/app.js @@ -19,7 +19,7 @@ var app = new function(){} app.init = function () { app.tube = new Tube () - app.master = new MasterView() + app.router = new Router () } app.launch = function () { diff --git a/public/assets/javascripts/ui/EditProfileModal.js b/public/assets/javascripts/ui/EditProfileModal.js index 5b79a31..6b89ad8 100644 --- a/public/assets/javascripts/ui/EditProfileModal.js +++ b/public/assets/javascripts/ui/EditProfileModal.js @@ -2,6 +2,7 @@ var EditProfileModal = ModalFormView.extend({ el: ".mediaDrawer.editProfile", action: "/api/profile", + method: "put", load: function(){ this.reset() @@ -12,13 +13,44 @@ var EditProfileModal = ModalFormView.extend({ this.$("[name='" + i + "']").val(data[i]) } + this.$("#profile_username").html(data.username) + + if (data.photo && data.photo.length) { + this.$("#load_avatar").attr("src", data.photo) + } + else { + this.$("#load_avatar").hide() + } + this.show() }, this)) }, + + validate: function(){ + var errors = [] + + var email = this.$("#profile_email").val() + var pw0 = this.$("#profile_old_password").val() + var pw1 = this.$("#profile_new_password").val() + var pw2 = this.$("#profile_new_password2").val() + + if (pw1.length) { + if (! pw0.length) { + errors.push("Please enter your old password.") + } + if (pw1 !== pw2) { + errors.push("New passwords don't match"); + } + } + if (email.length && email.indexOf("@") === -1) { + errors.push("Please enter a valid email address"); + } + + return errors + }, success: function(){ window.location.href = "/profile" } }) - diff --git a/public/assets/javascripts/ui/MasterView.js b/public/assets/javascripts/ui/MasterView.js deleted file mode 100644 index 5aca4e0..0000000 --- a/public/assets/javascripts/ui/MasterView.js +++ /dev/null @@ -1,77 +0,0 @@ - -var MasterView = View.extend({ - el: "body", - - events: { - "click [data-role='show-signup-modal']": 'signup', - "click [data-role='show-signin-modal']": 'signin', - "click [data-role='new-project-modal']": 'newProject', - "click [data-role='edit-project-modal']": 'editProject', - "click [data-role='edit-profile-modal']": 'editProfile', - }, - - routes: { - "/login": 'signin', - "/signup": 'signup', - "/project/new": 'newProject', - "/profile/edit": 'editProfile', - "/app": 'launch', - }, - - initialize: function(){ - this.signUpModal = new SignUpModal() - this.signInModal = new SignInModal() - this.newProjectModal = new NewProjectModal() - this.editProjectModal = new EditProjectModal() - this.editProfileModal = new EditProfileModal() - - this.originalPath = window.location.pathname - - for (var route in this.routes) { - if (window.location.pathname.indexOf(route) === 0) { - this[this.routes[route]]() - break; - } - } - - $("body").removeClass("loading") - }, - - launch: function(){ - app.launch() - }, - - signup: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/signup") - this.signUpModal.load() - }, - - signin: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/login") - this.signInModal.load() - }, - - newProject: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/project/new") - - this.newProjectModal.load() - }, - - editProject: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/project/edit") - this.editProjectModal.load() - }, - - editProfile: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/profile/edit") - - this.editProfileModal.load() - }, - -}) - diff --git a/public/assets/javascripts/ui/ModalFormView.js b/public/assets/javascripts/ui/ModalFormView.js deleted file mode 100644 index 608b8c1..0000000 --- a/public/assets/javascripts/ui/ModalFormView.js +++ /dev/null @@ -1,70 +0,0 @@ - -var ModalFormView = ModalView.extend({ - - events: { - "submit form": "submit" - }, - - initialize: function(){ - this.$form = this.$("form") - this.$errors = this.$(".errors") - this.$errorList = this.$(".errorList") - }, - - reset: function(){ - this.$("input").not("[type='submit']").not("[type='hidden']").val("") - }, - - load: function(){ - this.reset() - this.show() - }, - - showErrors: function(errors){ - if (errors && errors.length) { - this.$errors.show(); - for (var i in errors) { - this.$errorList.append('
' + errors[i] + '
'); - } - } - }, - - submit: function(e){ - e.preventDefault() - - this.$errors.hide(); - this.$errorList.empty(); - - if (this.validate) { - var errors = this.validate() - if (errors && errors.length) { - this.showErrors(errors) - return - } - } - - var fields = this.$form.serializeArray() - fields.forEach(function(pair){ - if (pair.name == "password" && pair.value.length > 0) { - pair.value = SHA1.hex('lol$' + pair.value + '$vvalls') - } - }) - - var request = $.post(this.action, $.param(fields)); - request.done($.proxy(function (response) { - if (response.error) { - this.$errors.show(); - var errors = [] - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); - } - this.showErrors(errors) - return - } - else { - this.success && this.success(response) - } - }, this)); - } - -}) diff --git a/public/assets/javascripts/ui/ModalView.js b/public/assets/javascripts/ui/ModalView.js deleted file mode 100644 index 80ce8d0..0000000 --- a/public/assets/javascripts/ui/ModalView.js +++ /dev/null @@ -1,28 +0,0 @@ - -var ModalView = View.extend({ - events: { - "click .close": 'close', - }, - - show: function(){ - $(".mediaDrawer").removeClass("active"); - this.$el.addClass("active"); - $("body").addClass("noOverflow"); - }, - - hide: function(){ - // $(".mediaDrawer, .room1").removeClass("active editing"); - this.$el.removeClass("active"); - $("body").removeClass("noOverflow"); - }, - - close: function(){ - if (window.isModalView) { - window.location.pathname = "/" - } - else { - history.pushState(null, document.title, app.master.originalPath) - this.hide() - } - } -}) diff --git a/public/assets/javascripts/ui/Router.js b/public/assets/javascripts/ui/Router.js new file mode 100644 index 0000000..a518e27 --- /dev/null +++ b/public/assets/javascripts/ui/Router.js @@ -0,0 +1,77 @@ + +var Router = View.extend({ + el: "body", + + events: { + "click [data-role='show-signup-modal']": 'signup', + "click [data-role='show-signin-modal']": 'signin', + "click [data-role='new-project-modal']": 'newProject', + "click [data-role='edit-project-modal']": 'editProject', + "click [data-role='edit-profile-modal']": 'editProfile', + }, + + routes: { + "/login": 'signin', + "/signup": 'signup', + "/project/new": 'newProject', + "/profile/edit": 'editProfile', + "/app": 'launch', + }, + + initialize: function(){ + this.signUpModal = new SignUpModal() + this.signInModal = new SignInModal() + this.newProjectModal = new NewProjectModal() + this.editProjectModal = new EditProjectModal() + this.editProfileModal = new EditProfileModal() + + this.originalPath = window.location.pathname + + for (var route in this.routes) { + if (window.location.pathname.indexOf(route) === 0) { + this[this.routes[route]]() + break; + } + } + + $("body").removeClass("loading") + }, + + launch: function(){ + app.launch() + }, + + signup: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/signup") + this.signUpModal.load() + }, + + signin: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/login") + this.signInModal.load() + }, + + newProject: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/project/new") + + this.newProjectModal.load() + }, + + editProject: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/project/edit") + this.editProjectModal.load() + }, + + editProfile: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/profile/edit") + + this.editProfileModal.load() + }, + +}) + diff --git a/public/assets/javascripts/ui/SignUpModal.js b/public/assets/javascripts/ui/SignUpModal.js index 95b5837..5c651ee 100644 --- a/public/assets/javascripts/ui/SignUpModal.js +++ b/public/assets/javascripts/ui/SignUpModal.js @@ -1,5 +1,3 @@ - - var SignUpModal = ModalFormView.extend({ el: ".mediaDrawer.signup", action: "/auth/signup", diff --git a/public/assets/javascripts/vendor/ModalFormView.js b/public/assets/javascripts/vendor/ModalFormView.js new file mode 100644 index 0000000..16d63b2 --- /dev/null +++ b/public/assets/javascripts/vendor/ModalFormView.js @@ -0,0 +1,76 @@ + +var ModalFormView = ModalView.extend({ + + method: "put", + + events: { + "submit form": "submit" + }, + + initialize: function(){ + this.$form = this.$("form") + this.$errors = this.$(".errors") + this.$errorList = this.$(".errorList") + }, + + reset: function(){ + this.$("input").not("[type='submit']").not("[type='hidden']").val("") + }, + + load: function(){ + this.reset() + this.show() + }, + + showErrors: function(errors){ + if (errors && errors.length) { + this.$errors.show(); + for (var i in errors) { + this.$errorList.append('
' + errors[i] + '
'); + } + } + }, + + submit: function(e){ + e.preventDefault() + + this.$errors.hide(); + this.$errorList.empty(); + + if (this.validate) { + var errors = this.validate() + if (errors && errors.length) { + this.showErrors(errors) + return + } + } + + var fields = this.$form.serializeArray() + fields.forEach(function(pair){ + if (pair.name.indexOf("password") !== -1 && pair.value.length > 0) { + pair.value = SHA1.hex('lol$' + pair.value + '$vvalls') + } + }) + + var request = $.ajax({ + url: this.action, + type: this.method, + data: $.param(fields) + }); + request.done($.proxy(function (response) { + if (response.error) { + this.$errors.show(); + var errors = [] + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + this.showErrors(errors) + return + } + else { + this.success && this.success(response) + } + }, this)); + } + +}) diff --git a/public/assets/javascripts/vendor/ModalView.js b/public/assets/javascripts/vendor/ModalView.js new file mode 100644 index 0000000..b90b3c4 --- /dev/null +++ b/public/assets/javascripts/vendor/ModalView.js @@ -0,0 +1,28 @@ + +var ModalView = View.extend({ + events: { + "click .close": 'close', + }, + + show: function(){ + $(".mediaDrawer").removeClass("active"); + this.$el.addClass("active"); + $("body").addClass("noOverflow"); + }, + + hide: function(){ + // $(".mediaDrawer, .room1").removeClass("active editing"); + this.$el.removeClass("active"); + $("body").removeClass("noOverflow"); + }, + + close: function(){ + if (window.isModalView) { + window.location.pathname = "/" + } + else { + history.pushState(null, document.title, app.router.originalPath) + this.hide() + } + } +}) diff --git a/public/assets/stylesheets/app.css b/public/assets/stylesheets/app.css index 85ecdb4..4a17214 100755 --- a/public/assets/stylesheets/app.css +++ b/public/assets/stylesheets/app.css @@ -424,7 +424,7 @@ h5{ font-size: 16px; font-weight: 300; } -.profilepage .bio span:nth-of-type:after { content: ' · ' } +.profilepage .bio span:after { content: ' \00b7 ' } .profilepage .bio span:last-of-type:after { display: none; } @@ -1274,6 +1274,11 @@ form h3 { form .errors div { float: none; } +form li div div { + width: 210px; + text-align: left; + margin: 0 10px 10px 0; +} .video { diff --git a/server/index.js b/server/index.js index 34d5989..3f9aad0 100644 --- a/server/index.js +++ b/server/index.js @@ -62,7 +62,7 @@ app.all('*', middleware.ensureLocals); app.get('/', views.home); app.get('/login', views.modal); app.get('/signup', views.modal); -app.post('/auth/signin', auth.loggedIn('local')); +app.post('/auth/signin', auth.loggedInLocal); app.post('/auth/signup', auth.signup); app.get('/logout', auth.logout); app.get('/auth/twitter', auth.login('twitter')); diff --git a/server/lib/api.js b/server/lib/api.js index 9ff9f38..85c5b85 100644 --- a/server/lib/api.js +++ b/server/lib/api.js @@ -1,7 +1,6 @@ /* jshint node: true */ var passport = require('passport'), - _ = require('lodash'), Entities = require('html-entities').XmlEntities, entities = new Entities(), crypto = require('crypto'), @@ -19,13 +18,28 @@ var api = { res.json(err || user) }) }, + update: function(req, res){ - var data = req.cleanQuery(req.body) - if (data.new_password.length && req.user.checkPassword(data.old_password)) { - data.password = data.new_password + var data = util.cleanQuery(req.body) + if (data.new_password.length) { + if (req.user.checkPassword(data.old_password)) { + res.json({ error: { errors: { username: { message: "Old password is incorrect" } } } }) + } + + var shasum = crypto.createHash('sha1') + shasum.update(data.new_password) + password = shasum.digest('hex'); + + req.user.password = password } delete data.old_password delete data.new_password + delete data.isAdmin + _.extend( req.user, data ) + req.user.save(function(err, msg) { + err ? res.json({ status: "FAIL", error: err }) + : res.json({ status: "OK", payload: req.user }) + }) } } diff --git a/server/lib/auth.js b/server/lib/auth.js index e7b7a75..5a952f5 100644 --- a/server/lib/auth.js +++ b/server/lib/auth.js @@ -72,7 +72,22 @@ var auth = { failureRedirect: '/login' }); }, - + + loggedInLocal: function (req, res, next) { + passport.authenticate("local", function(err, user, info){ + if (err) { + return res.json({ error: err }); + } + if (! user) { + return info ? res.json(info) : res.redirect("/login"); + } + req.logIn(user, function(err) { + if (err) { return next(err); } + return res.json({ status: "OK" }) + }); + })(req, res, next); + }, + logout: function (req, res) { req.logout(); res.redirect('/'); diff --git a/server/lib/util.js b/server/lib/util.js index 7a63507..88d16cb 100644 --- a/server/lib/util.js +++ b/server/lib/util.js @@ -1,4 +1,6 @@ +var _ = require('lodash'); + var whitespaceHead = /^\s+/ var whitespaceTail = /\s+$/ diff --git a/server/lib/views.js b/server/lib/views.js index 4f2402b..224dd3f 100644 --- a/server/lib/views.js +++ b/server/lib/views.js @@ -18,15 +18,12 @@ views.home = function (req, res) { } views.profile = function (req, res) { - var username = req.params[0] + var username = req.params[0] || req.user.username if (username) { User.findOne({ username: username }, function (err, user) { user ? next(user) : done(err, {}, []) }) } - else if (req.user) { - next(req.user) - } else { done() } diff --git a/views/partials/edit-profile.ejs b/views/partials/edit-profile.ejs index a9a5a9a..1a288db 100644 --- a/views/partials/edit-profile.ejs +++ b/views/partials/edit-profile.ejs @@ -3,13 +3,19 @@
- +
  • Edit Profile

  • - + +
    +
    +
    +
  • +
  • +
    @@ -20,6 +26,12 @@
+
  • + +
    + +
    +
  • @@ -29,13 +41,13 @@
  • - +
  • - +
  • @@ -43,10 +55,11 @@
    -

    please choose a picture at least 500px wide

    +

    please choose a picture at least 500px wide

  • +
  • -

    Edit Password

    +

    Change Password

  • @@ -55,9 +68,9 @@
  • - +
    - +
  • diff --git a/views/partials/header.ejs b/views/partials/header.ejs index c706aa1..e0e98c2 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -2,7 +2,7 @@ [[ if (logged_in) { ]] - [[ if (profile && user._id == profile._id) { ]] + [[ if (profile && String(user._id) == String(profile._id)) { ]] Edit Profile [[ } else if (! profile) { ]] View Profile diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs index fdb2229..5501c26 100644 --- a/views/partials/scripts.ejs +++ b/views/partials/scripts.ejs @@ -43,9 +43,10 @@ - - - + + + + -- cgit v1.2.3-70-g09d2