Skip to content

Commit

Permalink
Make authentication_token_created_at field optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Oelke committed Jun 23, 2016
2 parents 255cb45 + f13b089 commit 99bee7d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ This gem can be configured as shown in the following:
```ruby
Devise::TokenAuthenticatable.setup do |config|
# enables the expiration of a token after a specified amount of time,
# requires an additional field on the model: `authentication_token_created_at`
# defaults to nil
config.token_expires_in = 1.day

Expand Down
10 changes: 8 additions & 2 deletions lib/devise/token_authenticatable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ def authentication_token
end

def self.required_fields(klass)
[:authentication_token, :authentication_token_created_at]
fields = [:authentication_token]

unless Devise::TokenAuthenticatable.token_expires_in.blank?
fields.push(:authentication_token_created_at)
end

fields
end

# Generate new authentication token (a.k.a. "single access token").
def reset_authentication_token
self.authentication_token = self.class.authentication_token
self.authentication_token_created_at = Time.now
self.authentication_token_created_at = Time.now unless token_expires_in.blank?
end

# Generate new authentication token and save the record.
Expand Down
2 changes: 1 addition & 1 deletion lib/devise/token_authenticatable/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Devise
module TokenAuthenticatable
VERSION = '0.5.0'.freeze
VERSION = '0.5.1'.freeze
end
end
36 changes: 30 additions & 6 deletions spec/models/devise/token_authenticatable/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@
expect { subject }.to change { entity.authentication_token }
end

it "should reset token created at" do
expect { subject }.to change { entity.authentication_token_created_at }
context "token created at" do
it "should reset" do
swap Devise::TokenAuthenticatable, token_expires_in: 1.hour do
expect { subject }.to change { entity.authentication_token_created_at }
end
end

it "should not reset when token expires in not set" do
expect { subject }.to_not change { entity.authentication_token_created_at }
end
end
end

Expand Down Expand Up @@ -50,8 +58,16 @@
expect { subject }.to change { entity.authentication_token }
end

it "should set authentication token created at" do
expect { subject }.to change { entity.authentication_token_created_at }
context "token created at" do
it "should set" do
swap Devise::TokenAuthenticatable, token_expires_in: 1.hour do
expect { subject }.to change { entity.authentication_token_created_at }
end
end

it "should not set when token expires in disabled" do
expect { subject }.to_not change { entity.authentication_token_created_at }
end
end
end
end
Expand Down Expand Up @@ -89,11 +105,19 @@
end

describe "#required_fields" do
it "should contain the fields that Devise uses" do
it "should contain the fields that Devise uses when token expires in disabled" do
expect(Devise::Models::TokenAuthenticatable.required_fields(described_class)).to eq([
:authentication_token, :authentication_token_created_at
:authentication_token
])
end

it "should contain the fields that Devise uses" do
swap Devise::TokenAuthenticatable, token_expires_in: 1.hour do
expect(Devise::Models::TokenAuthenticatable.required_fields(described_class)).to eq([
:authentication_token, :authentication_token_created_at
])
end
end
end

end
Expand Down

0 comments on commit 99bee7d

Please sign in to comment.