diff --git a/app/assets/javascripts/password_resets.js.coffee b/app/assets/javascripts/password_resets.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/password_resets.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/password_resets.css.scss b/app/assets/stylesheets/password_resets.css.scss new file mode 100644 index 0000000..8160ad8 --- /dev/null +++ b/app/assets/stylesheets/password_resets.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the password_resets controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/password_resets_controller.rb b/app/controllers/password_resets_controller.rb new file mode 100644 index 0000000..1493f99 --- /dev/null +++ b/app/controllers/password_resets_controller.rb @@ -0,0 +1,29 @@ +class PasswordResetsController < ApplicationController + skip_before_filter :authenticated, :only => [:reset_password] + + def reset_password + token = params[:token] unless params[:token].nil? + + if token && is_valid?(token) + flash[:success] = "Password reset token confirmed! Please create a new password." + #redirect_to :reset_password + else + flash[:error] = "Invalid password reset token. Please try again." + redirect_to :login + end + end + + private + + def is_valid?(token) + if token =~ /(?\d+)-(?[A-Z0-9]{32})/i + + # Fetch the user by their id, and hash their email address + user = User.find_by_id($~[:user_id]) + email = Digest::MD5.hexdigest(user.email) + + # Compare and validate our hashes + return true if email == $~[:email_hash] + end + end +end diff --git a/app/helpers/password_resets_helper.rb b/app/helpers/password_resets_helper.rb new file mode 100644 index 0000000..0c9d96e --- /dev/null +++ b/app/helpers/password_resets_helper.rb @@ -0,0 +1,2 @@ +module PasswordResetsHelper +end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 1c61b83..209b839 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -3,6 +3,8 @@ class UserMailer < ActionMailer::Base def forgot_password(email, token) @token = token - mail(to: "#{email}", subject: "Reset your RailsGoat password") + @url = url_for(controller: "password_resets", action: "reset_password", only_path: false) + "?token=#{token}" + + mail(to: "#{email}", subject: "Reset your MetaCorp password") end end diff --git a/app/views/password_resets/reset_password.html.erb b/app/views/password_resets/reset_password.html.erb new file mode 100644 index 0000000..0cd2fe8 --- /dev/null +++ b/app/views/password_resets/reset_password.html.erb @@ -0,0 +1,38 @@ +
+

MetaCorp

+

A GoatGroup Company

+
+
+
+ + + + + + + +
+
+
+
\ No newline at end of file diff --git a/app/views/user_mailer/forgot_password.html.erb b/app/views/user_mailer/forgot_password.html.erb index a9f5913..ca2fe76 100644 --- a/app/views/user_mailer/forgot_password.html.erb +++ b/app/views/user_mailer/forgot_password.html.erb @@ -6,8 +6,17 @@

Need help logging in?

- To reset your RailsGoat password, simply click on the - following link and follow the instructions: <%= @token %>.
+ A password reset was requested for your user account.
+
+ + To reset your MetaCorp password, simply click on the + following link and follow the instructions:
+
+ + <%= link_to "Click here to reset your password", @url %>
+
+ + If you don't want to change your password, you can ignore this email.

Thanks, and have a great day!

diff --git a/app/views/user_mailer/forgot_password.text.erb b/app/views/user_mailer/forgot_password.text.erb index cdc672e..92067db 100644 --- a/app/views/user_mailer/forgot_password.text.erb +++ b/app/views/user_mailer/forgot_password.text.erb @@ -1,7 +1,13 @@ Need help logging in? ========================================================== -To reset your RailsGoat password, simply click on the -following link and follow the instructions: <%= @token %>. +A password reset was requested for your user account. + +To reset your MetaCorp password, simply copy the +following link and follow the instructions: + +<%= @url %> + +If you don't want to change your password, you can ignore this email. Thanks, and have a great day! \ No newline at end of file diff --git a/app/views/users/forgot_password.html.erb b/app/views/users/forgot_password.html.erb index 6e7408a..be3d8a3 100644 --- a/app/views/users/forgot_password.html.erb +++ b/app/views/users/forgot_password.html.erb @@ -19,7 +19,7 @@
- <%= submit_tag "Send Forgot Password Email", {:class => "btn btn-danger btn-large"} %> + <%= submit_tag "Reset Password", {:class => "btn btn-danger btn-large"} %>
diff --git a/config/environments/development.rb b/config/environments/development.rb index 27e657b..39b0f53 100755 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -38,6 +38,7 @@ Railsgoat::Application.configure do # ActionMailer settings for email support config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } + config.action_mailer.default_url_options = { :host => "localhost:3000" } config.middleware.insert_before( Rack::Lock, Rack::LiveReload, diff --git a/config/routes.rb b/config/routes.rb index 441d118..a9f6e25 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,9 +4,9 @@ Railsgoat::Application.routes.draw do get "signup" => "users#new" get "logout" => "sessions#destroy" match "forgot_password" => "users#forgot_password" + match "password_resets" => "password_resets#reset_password" resources :sessions do - end resources :users do diff --git a/spec/controllers/password_resets_controller_spec.rb b/spec/controllers/password_resets_controller_spec.rb new file mode 100644 index 0000000..41ab485 --- /dev/null +++ b/spec/controllers/password_resets_controller_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe PasswordResetsController do + + describe "GET 'new'" do + it "returns http success" do + get 'new' + response.should be_success + end + end + +end diff --git a/spec/helpers/password_resets_helper_spec.rb b/spec/helpers/password_resets_helper_spec.rb new file mode 100644 index 0000000..a0df3dd --- /dev/null +++ b/spec/helpers/password_resets_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the PasswordResetsHelper. For example: +# +# describe PasswordResetsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe PasswordResetsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/password_resets/new.html.erb_spec.rb b/spec/views/password_resets/new.html.erb_spec.rb new file mode 100644 index 0000000..38c6853 --- /dev/null +++ b/spec/views/password_resets/new.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe "password_resets/new.html.erb" do + pending "add some examples to (or delete) #{__FILE__}" +end