Skip to content

Commit bf5b172

Browse files
committed
Added password reset with perishable tokens
1 parent e31783b commit bf5b172

15 files changed

+159
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class PasswordResetsController < ApplicationController
2+
before_filter :require_no_user
3+
4+
def new
5+
render
6+
end
7+
8+
def create
9+
@user = User.find_by_email(params[:email])
10+
if @user
11+
@user.deliver_password_reset_instructions!
12+
flash[:notice] = "Instructions to reset your password have been emailed to you. " +
13+
"Please check your email."
14+
redirect_to root_url
15+
else
16+
flash[:notice] = "No user was found with that email address"
17+
render :action => :new
18+
end
19+
end
20+
21+
def edit
22+
render
23+
end
24+
25+
26+
def update
27+
@user.password = params[:user][:password]
28+
@user.password_confirmation = params[:user][: password_confirmation]
29+
if @user.save
30+
flash[:notice] = "Password successfully updated"
31+
redirect_to account_url
32+
else
33+
render :action => :edit
34+
end
35+
end
36+
37+
private
38+
39+
def load_user_using_perishable_token
40+
@user = User.find_using_perishable_token(params[:id])
41+
unless @user
42+
flash[:notice] = "We're sorry, but we could not locate your account. " +
43+
"If you are having issues try copying and pasting the URL " +
44+
"from your email into your browser or restarting the " +
45+
"reset password process."
46+
redirect_to root_url
47+
end
48+
end
49+
50+
end
51+
52+

app/helpers/password_resets_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PasswordResetsHelper
2+
end

app/models/notifier.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Notifier < ActiveRecord::Base
2+
default_url_options[:host] = "authlogic_example.binarylogic.com"
3+
4+
def password_reset_instructions(user)
5+
subject "Password Reset Instructions"
6+
from "Binary Logic Notifier "
7+
recipients user.email
8+
sent_on Time.now
9+
body :edit_password_reset_url => edit_password_reset_url(user.perishable_token)
10+
end
11+
12+
end

app/models/user.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
class User < ActiveRecord::Base
22
acts_as_authentic do |c|
33
end
4+
5+
def active?
6+
active
7+
end
8+
9+
def deliver_password_reset_instructions!
10+
reset_perishable_token!
11+
Notifier.deliver_password_reset_instructions(self)
12+
end
413
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
A request to reset your password has been made. If you did not make this request, simply ignore this email. If you did make this request just click the link below:
2+
3+
<%= @edit_password_reset_url %>
4+
5+
If the above URL does not work try copying and pasting it into your browser. If you continue to have problem please feel free to contact us.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<h1>Change My Password</h1>
2+
3+
<% form_for @user, :url => password_reset_path, :method => :put do |f| %>
4+
<%= f.error_messages %>
5+
<%= f.label :password %><br />
6+
<%= f.password_field :password %><br />
7+
<br />
8+
<%= f.label :password_confirmation %><br />
9+
<%= f.password_field :password_confirmation %><br />
10+
<br />
11+
<%= f.submit "Update my password and log me in" %>
12+
<% end %>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h1>Forgot Password</h1>
2+
3+
Fill out the form below and instructions to reset your password will be emailed to you:<br />
4+
<br />
5+
6+
<% form_tag password_resets_path do %>
7+
<label>Email:</label><br />
8+
<%= text_field_tag "email" %><br />
9+
<br />
10+
<%= submit_tag "Reset my password" %>
11+
<% end %>

app/views/user_sessions/new.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
<br />
1313
<%= f.submit "Login" %>
1414
<% end %>
15+
16+
<%= link_to "Forgot your password?", password_reset_path %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
map.root :controller => "user_sessions", :action => "new"
55
map.resource :account, :controller => "users"
66
map.resources :users
7+
map.resources :password_resets
78

89
# The priority is based upon order of creation: first created -> highest priority.
910

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddActiveToUsers < ActiveRecord::Migration
2+
def self.up
3+
add_column :users, :active, :boolean, :default => false, :null => false
4+
end
5+
6+
def self.down
7+
remove_column :users, :active
8+
end
9+
end

0 commit comments

Comments
 (0)