Skip to content

Commit

Permalink
Merge pull request maccman#1 from jclay/change_password
Browse files Browse the repository at this point in the history
Adding in change password functionality
  • Loading branch information
luminlabs committed Feb 4, 2014
2 parents ea2e6d0 + df92219 commit f38491b
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 42 deletions.
22 changes: 22 additions & 0 deletions app/assets/javascripts/app/controllers/alert.module.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$ = jQuery
Overlay = require('app/controllers/overlay')

class Alert extends Overlay
className: 'alert'

constructor: (options = {}) ->
super()
@on('click', '#dismiss', @close)
console.log(options.title)
@title = options.title
@message = options.message

@render()
@open()

render: =>
@html(@view('alert')(this))
@$('[data-name=title]').html(@title)
@$('[data-name=message]').html(@message)

module.exports = Alert
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ helpers = require('app/helpers')
State = require('app/state')
UserProfile = require('app/controllers/users/profile')
UserInvite = require('app/controllers/users/invite')
ChangePassword = require('app/controllers/users/change_password')
Feedback = require('app/controllers/feedback')

class UserMenu extends Controller
Expand All @@ -16,6 +17,7 @@ class UserMenu extends Controller
@on('click', 'a[data-name=profile]', @clickProfile)
@on('click', 'a[data-name=invite]', @clickInvite)
@on('click', 'a[data-name=feedback]', @clickFeedback)
@on('click', 'a[data-name=change_password]', @clickChangePassword)
@on('click', @cancel)
@user.observe(@render)
@render()
Expand Down Expand Up @@ -47,6 +49,11 @@ class UserMenu extends Controller
@close()
UserProfile.open(@user)

clickChangePassword: (e) =>
e.preventDefault()
@close()
ChangePassword.open(@user)

clickInvite: (e) =>
e.preventDefault()
@close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
$ = jQuery
Overlay = require('app/controllers/overlay')
User = require('app/models/user')
State = require('app/state')
Alert = require('app/controllers/alert')

class ChangePassword extends Overlay
className: 'users-change-password'

constructor: (@user) ->
super()
@on 'submit', @submit
@render()

render: =>
# @user = State.get('user')

@html(@view('users/change_password')(this))
@$form = @$('form')
@$errors = @$('#errors')

@$current_password = @$('input#current_password')
@$new_password = @$('input#new_password')
@$confirm_new_password = @$('input#confirm_new_password')

clearErrors: =>
@$errors.children("ul").html("")

addErrors: (errors) =>
@$errors.children("ul").html("")
for error in errors
@$errors.children("ul").append("<li>" + error + "</li>")

clearValues: =>
@$current_password.val("")
@$new_password.val("")
@$confirm_new_password.val("")

submit: (e) =>
e.preventDefault()

response = @user.change_password(@$current_password.val(),
@$new_password.val(),
@$confirm_new_password.val())


response.done (data, textStatus, jqXHR) =>
@close()
new Alert( title: "Success!", message: data)

response.fail (jqXHR, textStatus, errorThrown) =>
@clearValues()
errors = JSON.parse(jqXHR.responseText)
@$errors.show()
@clearErrors()
@addErrors(errors)

module.exports = ChangePassword
11 changes: 11 additions & 0 deletions app/assets/javascripts/app/models/user.module.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ class User extends Model

@request

change_password: (current_password, new_password, confirm_new_password) =>
@request = $.post(
@constructor.uri('change_password'),
user_id: @get('id')
current_password: current_password,
new_password: new_password,
confirm_new_password: confirm_new_password
)

@request

save: =>
@add()
@request = @set $.ajax
Expand Down
11 changes: 11 additions & 0 deletions app/assets/javascripts/app/views/alert.jst.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<header>
<h2 data-name="title"></h2>
</header>

<article>
<p data-name="message"></p>
</article>

<footer>
<button id="dismiss" class="btn">Ok</button>
</footer>
2 changes: 2 additions & 0 deletions app/assets/javascripts/app/views/sidebar/user_menu.jst.eco
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
</a>
</li>


<li><a data-name="feedback">Feedback</a></li>

<li class="divider"></li>
<li><a data-name="change_password">Change Password</a></li>

<li><a href="/logout">Logout</a></li>
35 changes: 35 additions & 0 deletions app/assets/javascripts/app/views/users/change_password.jst.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<header>
<h2>Change Password</h2>
</header>

<div id="errors" class="hidden">
<h4>Uh-oh! Some errors occured</h4>
<ul></ul>
</div>

<form>

<article>
<input id="current_password"
type="password"
name="current_password"
placeholder="Current Password"
autofocus>

<input id="new_password"
type="password"
name="new_password"
placeholder="New Password">

<input id="confirm_new_password"
type="password"
name="confirm_new_password"
placeholder="Confirm New Password">
</article>

<footer>
<button type="submit" class="btn">Change</button>
</footer>

</form>

34 changes: 34 additions & 0 deletions app/assets/stylesheets/app/users_change_password.css.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.overlay .panel.users-change-password {
width: 350px

article {
margin: 30px 30px 20px 30px
}

input {
margin-bottom: 10px
height: 38px
padding: 0 10px

&:last-child {
margin-bottom: 0
}
}

footer {
margin: 0 30px 25px 30px
display: block;
}

.divider {
text-align:center;
font-weight: 200;
}

button {
width: 100%
height: 34px
padding: 0 14px
font-size: 14px
}
}
42 changes: 0 additions & 42 deletions app/models/Find Results

This file was deleted.

22 changes: 22 additions & 0 deletions app/routes/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ class Users < Base
end
end

post '/v1/users/change_password' do
user = User.find(id: params[:user_id])

passwords_match = params[:new_password] == params[:confirm_new_password]

if user && passwords_match
if user.valid_password?(params[:current_password])
user.password = params[:new_password]

if user.save!
halt 200, ["Password was reset successfully!"]
else
halt 422, ["An error occurred while resetting your password."]
end
else
halt 403, ["current password was incorrect"].to_json
end
else
halt 422, ["New password and confirmation did not match!"].to_json
end
end

post '/v1/users/forgot_password' do
unless params[:email].present?
error 422
Expand Down

0 comments on commit f38491b

Please sign in to comment.