Skip to content

Commit

Permalink
Fix overwriting the remember_token when a valid one already exists (h…
Browse files Browse the repository at this point in the history
…eartcombo#4101)

The remember_token should not get overwritten when a user is
signing in and a valid token already exists.

Fixes heartcombo#3950.
  • Loading branch information
Ralin Chimev authored and ulissesalmeida committed May 15, 2016
1 parent 2044fff commit 9caf07d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/devise/models/rememberable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.required_fields(klass)
end

def remember_me!
self.remember_token = self.class.remember_token if respond_to?(:remember_token)
self.remember_token ||= self.class.remember_token if respond_to?(:remember_token)
self.remember_created_at ||= Time.now.utc
save(validate: false) if self.changed?
end
Expand Down
12 changes: 12 additions & 0 deletions test/models/rememberable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def create_resource
assert user.remember_created_at
end

test 'remember_me should not generate a new token if valid token exists' do
user = create_user
user.singleton_class.send(:attr_accessor, :remember_token)
User.to_adapter.expects(:find_first).returns(nil)

user.remember_me!
existing_token = user.remember_token

user.remember_me!
assert_equal existing_token, user.remember_token
end

test 'forget_me should not clear remember token if using salt' do
user = create_user
user.remember_me!
Expand Down

0 comments on commit 9caf07d

Please sign in to comment.