Implement working mailer and controller

This commit is contained in:
James Espinosa
2013-12-04 00:57:32 -06:00
parent 1a3d6d690c
commit da1845e8f9
14 changed files with 132 additions and 7 deletions
@@ -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/
@@ -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/
@@ -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 =~ /(?<user_id>\d+)-(?<email_hash>[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
+2
View File
@@ -0,0 +1,2 @@
module PasswordResetsHelper
end
+3 -1
View File
@@ -3,6 +3,8 @@ class UserMailer < ActionMailer::Base
def forgot_password(email, token) def forgot_password(email, token)
@token = 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
end end
@@ -0,0 +1,38 @@
<div class="row-fluid">
<h2 align="center">MetaCorp</h2>
<h3 align="center">A GoatGroup Company</h3>
<div class="span12">
<div class="row-fluid">
<div class="span4 offset4">
<!-- TODO -->
<!-- Create a form that allows a user to reset their password -->
<!-- This form is just a placeholder with no working functionality -->
<div class="signup">
<%= form_tag "reset_passwords", :class=> "signup-wrapper" do %>
<div class="header">
<h2>Create Password</h2>
<p>Fill out the form below to create a new password.</p>
</div>
<div class="content">
<%= label_tag "Enter Password" %>
<%= password_field_tag :password, params[:password], {:class => "input input-block-level"} %>
<%= label_tag "Confirm Password" %>
<%= password_field_tag :confirm_password, params[:confirm_password], {:class => "input input-block-level"} %>
</div>
<div class="actions">
<%= submit_tag "Create Password", {:class => "btn btn-danger btn-large"} %>
</div>
<div class="clearfix"></div>
<% end %>
</div>
</div>
</div>
</div>
</div>
+11 -2
View File
@@ -6,8 +6,17 @@
<body> <body>
<h1>Need help logging in?</h1> <h1>Need help logging in?</h1>
<p> <p>
To reset your RailsGoat password, simply click on the A password reset was requested for your user account.<br>
following link and follow the instructions: <%= @token %>.<br/> <br>
To reset your MetaCorp password, simply click on the
following link and follow the instructions:<br>
<br>
<%= link_to "Click here to reset your password", @url %><br>
<br>
If you don't want to change your password, you can ignore this email.
</p> </p>
<p>Thanks, and have a great day!</p> <p>Thanks, and have a great day!</p>
</body> </body>
@@ -1,7 +1,13 @@
Need help logging in? Need help logging in?
========================================================== ==========================================================
To reset your RailsGoat password, simply click on the A password reset was requested for your user account.
following link and follow the instructions: <%= @token %>.
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! Thanks, and have a great day!
+1 -1
View File
@@ -19,7 +19,7 @@
</div> </div>
<div class="actions"> <div class="actions">
<%= submit_tag "Send Forgot Password Email", {:class => "btn btn-danger btn-large"} %> <%= submit_tag "Reset Password", {:class => "btn btn-danger btn-large"} %>
</div> </div>
<div class="clearfix"></div> <div class="clearfix"></div>
+1
View File
@@ -38,6 +38,7 @@ Railsgoat::Application.configure do
# ActionMailer settings for email support # ActionMailer settings for email support
config.action_mailer.delivery_method = :smtp config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.middleware.insert_before( config.middleware.insert_before(
Rack::Lock, Rack::LiveReload, Rack::Lock, Rack::LiveReload,
+1 -1
View File
@@ -4,9 +4,9 @@ Railsgoat::Application.routes.draw do
get "signup" => "users#new" get "signup" => "users#new"
get "logout" => "sessions#destroy" get "logout" => "sessions#destroy"
match "forgot_password" => "users#forgot_password" match "forgot_password" => "users#forgot_password"
match "password_resets" => "password_resets#reset_password"
resources :sessions do resources :sessions do
end end
resources :users do resources :users do
@@ -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
@@ -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
@@ -0,0 +1,5 @@
require 'spec_helper'
describe "password_resets/new.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end