Skip to content

Commit

Permalink
Removed unnecessary remember_token_expires_at column and the remember…
Browse files Browse the repository at this point in the history
…? and forget_me! user instance methods
  • Loading branch information
Daniel Croak committed Aug 31, 2009
1 parent cbcc227 commit 9a088ba
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 84 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.textile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
h2. 0.8.1 (08/31/2009)

* Removed unnecessary remember_token_expires_at column and the
remember? and forget_me! user instance methods. (Dan Croak)

h2. 0.8.0 (08/31/2009)

* Always remember me. Replaced session-and-remember-me authentication with
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/clearance/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create
end

def destroy
sign_out(current_user)
sign_out
flash_success_after_destroy
redirect_to(url_after_destroy)
end
Expand Down
1 change: 0 additions & 1 deletion generators/clearance/templates/migrations/create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def self.up
t.string :salt, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128
t.datetime :remember_token_expires_at
t.boolean :email_confirmed, :default => false, :null => false
t.timestamps
end
Expand Down
1 change: 0 additions & 1 deletion generators/clearance/templates/migrations/update_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def self.up
[:salt, 't.string :salt, :limit => 128'],
[:confirmation_token, 't.string :confirmation_token, :limit => 128'],
[:remember_token, 't.string :remember_token, :limit => 128'],
[:remember_token_expires_at, 't.datetime :remember_token_expires_at'],
[:email_confirmed, 't.boolean :email_confirmed, :default => false, :null => false']
].delete_if {|c| existing_columns.include?(c.first.to_s)}
-%>
Expand Down
17 changes: 7 additions & 10 deletions lib/clearance/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,17 @@ def sign_in(user)
user.remember_me!
cookies[:remember_token] = {
:value => user.remember_token,
:expires => user.remember_token_expires_at
:expires => 1.year.from_now.utc
}
end
end

# Sign user out of cookie.
#
# @param [User]
#
# @example
# sign_out(@user)
def sign_out(user)
user.forget_me! if user
# sign_out
def sign_out
cookies.delete(:remember_token)
reset_session
end

# Store the current location.
Expand All @@ -83,8 +79,7 @@ def deny_access(flash_message = nil)

def user_from_cookie
if token = cookies[:remember_token]
return nil unless user = ::User.find_by_remember_token(token)
return user if user.remember?
::User.find_by_remember_token(token)
end
end

Expand All @@ -94,7 +89,9 @@ def sign_user_in(user)
end

def store_location
session[:return_to] = request.request_uri if request.get?
if request.get?
session[:return_to] = request.request_uri
end
end

def redirect_back_or(default)
Expand Down
28 changes: 1 addition & 27 deletions lib/clearance/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ def authenticated?(password)
encrypted_password == encrypt(password)
end

# Am I remembered?
#
# @return [true, false]
# @example
# user.remember?
def remember?
remember_token &&
remember_token_expires_at &&
Time.now.utc < remember_token_expires_at
end

# Remember me for a year.
#
# @example
Expand All @@ -121,16 +110,7 @@ def remember?
# :expires => user.remember_token_expires_at
# }
def remember_me!
remember_me_until! 1.year.from_now.utc
end

# Forget me.
#
# @example
# user.forget_me!
def forget_me!
self.remember_token = nil
self.remember_token_expires_at = nil
self.remember_token = encrypt("--#{Time.now.utc}--#{password}--")
save(false)
end

Expand Down Expand Up @@ -200,12 +180,6 @@ def initialize_confirmation_token
def password_required?
encrypted_password.blank? || !password.blank?
end

def remember_me_until!(time)
self.remember_token_expires_at = time
self.remember_token = encrypt("--#{time}--#{password}--")
save(false)
end
end

module ClassMethods
Expand Down
2 changes: 0 additions & 2 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class SessionsControllerTest < ActionController::TestCase

should 'set the token in users table' do
assert_not_nil @user.reload.remember_token
assert_not_nil @user.reload.remember_token_expires_at
end
end

Expand Down Expand Up @@ -136,7 +135,6 @@ class SessionsControllerTest < ActionController::TestCase

should "delete the database token" do
assert_nil @user.reload.remember_token
assert_nil @user.reload.remember_token_expires_at
end
end

Expand Down
44 changes: 2 additions & 42 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
class UserTest < ActiveSupport::TestCase

should_not_allow_mass_assignment_of :email_confirmed,
:salt, :encrypted_password,
:remember_token, :remember_token_expires_at
:salt, :encrypted_password, :remember_token

# signing up

Expand Down Expand Up @@ -113,50 +112,11 @@ def @user.initialize_salt; end
setup do
@user = Factory(:email_confirmed_user)
assert_nil @user.remember_token
assert_nil @user.remember_token_expires_at
@user.remember_me!
end

should "set the remember token and expiration date" do
should "set the remember token" do
assert_not_nil @user.remember_token
assert_not_nil @user.remember_token_expires_at
end

should "remember user when remember token expires in the future" do
@user.update_attribute :remember_token_expires_at,
2.weeks.from_now.utc
assert @user.remember?
end

should "not remember user when remember token has already expired" do
@user.update_attribute :remember_token_expires_at,
2.weeks.ago.utc
assert ! @user.remember?
end

should "not remember user when remember token is not set" do
@user.update_attribute :remember_token, nil
assert ! @user.remember?
end

should "not remember user when remember token expiry date is not set" do
@user.update_attribute :remember_token_expires_at, nil
assert ! @user.remember?
end

# logging out

context "forget_me!" do
setup { @user.forget_me! }

should "unset the remember token and expiration date" do
assert_nil @user.remember_token
assert_nil @user.remember_token_expires_at
end

should "not remember user" do
assert ! @user.remember?
end
end
end

Expand Down

0 comments on commit 9a088ba

Please sign in to comment.