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..eb9649c --- /dev/null +++ b/app/assets/stylesheets/password_resets.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the PasswordResets 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..a2861ea --- /dev/null +++ b/app/controllers/password_resets_controller.rb @@ -0,0 +1,25 @@ +class PasswordResetsController < ApplicationController + def create + @user = User.find_by_email(params[:email]) + @user.deliver_reset_password_instructions! if @user + redirect_to(root_path, :notice => "Instructions have been sent to your email.") + end + + def edit + @user = User.load_from_reset_password_token(params[:id]) + @token = params[:id] + not_authenticated unless @user + end + + def update + @token = params[:token] + @user = User.load_from_reset_password_token(params[:token]) + not_authenticated unless @user + @user.password_confirmation = params[:user][:password_confirmation] + if @user.change_password!(params[:user][:password]) + redirect_to(root_path, :notice => "Password changed OK") + else + render :action => "edit" + 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 new file mode 100644 index 0000000..8d62cb1 --- /dev/null +++ b/app/mailers/user_mailer.rb @@ -0,0 +1,14 @@ +class UserMailer < ActionMailer::Base + default from: "from@example.com" + + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.user_mailer.reset_password_email.subject + # + def reset_password_email(user) + @user = user + @url = "http://localhost:3000/password_resets/#{user.reset_password_token}/edit" + mail(:to => user.email, :subject => "Your password has been reset") + end +end diff --git a/app/views/password_resets/create.html.haml b/app/views/password_resets/create.html.haml new file mode 100644 index 0000000..bb78a4e --- /dev/null +++ b/app/views/password_resets/create.html.haml @@ -0,0 +1,2 @@ +%h1 PasswordResets#create +%p Find me in app/views/password_resets/create.html.haml \ No newline at end of file diff --git a/app/views/password_resets/edit.html.haml b/app/views/password_resets/edit.html.haml new file mode 100644 index 0000000..a128c35 --- /dev/null +++ b/app/views/password_resets/edit.html.haml @@ -0,0 +1,32 @@ +%h1 Choose a new password + += form_for @user, :url => password_reset_path(@user), :html => { :method => :put } do |f| + - if @user.errors.any? + #error_explanation + %h2 + = pluralize(@user.errors.count, "error") + prohibited this user from being saved: + + %ul + - @user.errors.full_messages.each do |msg| + %li= msg + + .field + = f.label :email + %br + = @user.email + + .field + = f.label :password + %br + = f.password_field :password + + .field + = f.label :password_confirmation + %br + = f.password_field :password_confirmation + = hidden_field_tag :token, @token + + .actions + = f.submit + \ No newline at end of file diff --git a/app/views/password_resets/update.html.haml b/app/views/password_resets/update.html.haml new file mode 100644 index 0000000..25f3bfa --- /dev/null +++ b/app/views/password_resets/update.html.haml @@ -0,0 +1,2 @@ +%h1 PasswordResets#update +%p Find me in app/views/password_resets/update.html.haml \ No newline at end of file diff --git a/app/views/sessions/_forgot_password_form.html.haml b/app/views/sessions/_forgot_password_form.html.haml new file mode 100644 index 0000000..c60b8cd --- /dev/null +++ b/app/views/sessions/_forgot_password_form.html.haml @@ -0,0 +1,7 @@ += form_tag password_resets_path, :method => :post do + .field + = label_tag :email + %br + = text_field_tag :email + = submit_tag "Reset my password" + \ No newline at end of file diff --git a/app/views/sessions/new.html.haml b/app/views/sessions/new.html.haml index 900eca5..2b84758 100644 --- a/app/views/sessions/new.html.haml +++ b/app/views/sessions/new.html.haml @@ -16,4 +16,8 @@ = label_tag :remember_me .actions - = submit_tag "Log in" \ No newline at end of file + = submit_tag "Log in" + +%h1 Forgotten your password? + += render "forgot_password_form" diff --git a/app/views/user_mailer/reset_password_email.text.haml b/app/views/user_mailer/reset_password_email.text.haml new file mode 100644 index 0000000..d6b1a63 --- /dev/null +++ b/app/views/user_mailer/reset_password_email.text.haml @@ -0,0 +1,10 @@ +Hello, += @user.email + +You have requested to reset your password. + +To choose a new password, just follow this link: + += @url + +Have a great day! diff --git a/config/environments/development.rb b/config/environments/development.rb index 8052e60..8193158 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -28,4 +28,12 @@ Apollo::Application.configure do # Expands the lines which load the assets config.assets.debug = true + + config.action_mailer.delivery_method = :smtp + + ActionMailer::Base.smtp_settings = { + :address => "localhost", + :port => 1025, + :domain => "localhost:3000" + } end diff --git a/config/initializers/sorcery.rb b/config/initializers/sorcery.rb index 72fd06a..22ea716 100644 --- a/config/initializers/sorcery.rb +++ b/config/initializers/sorcery.rb @@ -3,7 +3,7 @@ # Available submodules are: :user_activation, :http_basic_auth, :remember_me, # :reset_password, :session_timeout, :brute_force_protection, :activity_logging, :external # Rails.application.config.sorcery.submodules = [:remember_me, :reset_password] -Rails.application.config.sorcery.submodules = [:remember_me] +Rails.application.config.sorcery.submodules = [:remember_me, :reset_password] # Here you can configure each submodule's features. Rails.application.config.sorcery.configure do |config| @@ -269,7 +269,7 @@ Rails.application.config.sorcery.configure do |config| # mailer class. Needed. # Default: `nil` # - # user.reset_password_mailer = + user.reset_password_mailer = UserMailer # reset password email method on your mailer class. diff --git a/config/routes.rb b/config/routes.rb index 64f4f28..6cbea69 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Apollo::Application.routes.draw do resources :users resources :sessions + resources :password_resets get "posts/near" diff --git a/test/functional/password_resets_controller_test.rb b/test/functional/password_resets_controller_test.rb new file mode 100644 index 0000000..206ad7f --- /dev/null +++ b/test/functional/password_resets_controller_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class PasswordResetsControllerTest < ActionController::TestCase + test "should get create" do + get :create + assert_response :success + end + + test "should get edit" do + get :edit + assert_response :success + end + + test "should get update" do + get :update + assert_response :success + end + +end diff --git a/test/functional/user_mailer_test.rb b/test/functional/user_mailer_test.rb new file mode 100644 index 0000000..5d4d72c --- /dev/null +++ b/test/functional/user_mailer_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class UserMailerTest < ActionMailer::TestCase + test "reset_password_email" do + mail = UserMailer.reset_password_email + assert_equal "Reset password email", mail.subject + assert_equal ["to@example.org"], mail.to + assert_equal ["from@example.com"], mail.from + assert_match "Hi", mail.body.encoded + end + +end diff --git a/test/unit/helpers/password_resets_helper_test.rb b/test/unit/helpers/password_resets_helper_test.rb new file mode 100644 index 0000000..1a5257c --- /dev/null +++ b/test/unit/helpers/password_resets_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class PasswordResetsHelperTest < ActionView::TestCase +end