Skip to content

Commit

Permalink
Allow custom attribute name for created_at
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaa committed Jan 22, 2025
1 parent a4ffd6f commit 733991f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/ash_authentication/token_resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ defmodule AshAuthentication.TokenResource do
The Ash domain to use to access this resource.
"""
],
created_at_attribute_name: [
type: :atom,
doc: "The name of the `created_at` attribute on this resource.",
default: :created_at
],
expunge_expired_action_name: [
type: :atom,
doc: """
Expand Down
4 changes: 3 additions & 1 deletion lib/ash_authentication/token_resource/transformer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ defmodule AshAuthentication.TokenResource.Transformer do
public?: true
),
:ok <- validate_jti_field(dsl_state),
{:ok, created_at} <-
TokenResource.Info.token_created_at_attribute_name(dsl_state),
{:ok, dsl_state} <-
maybe_build_attribute(dsl_state, :subject, :string, allow_nil?: false, writable?: true),
:ok <- validate_subject_field(dsl_state),
Expand All @@ -68,7 +70,7 @@ defmodule AshAuthentication.TokenResource.Transformer do
public?: true
),
{:ok, dsl_state} <-
maybe_build_attribute(dsl_state, :created_at, :utc_datetime_usec,
maybe_build_attribute(dsl_state, created_at, :utc_datetime_usec,
allow_nil?: false,
public?: false,
default: &DateTime.utc_now/0
Expand Down
6 changes: 6 additions & 0 deletions test/ash_authentication/token_resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ defmodule AshAuthentication.TokenResourceTest do
end
end

test "uses created_at" do
attribute = Ash.Resource.Info.attribute(Example.TokenWithCustomCreateTimestamp, :created_at)

assert :inserted_at = attribute.source
end

def build_token do
{:ok, token, claims} =
build_user()
Expand Down
1 change: 1 addition & 0 deletions test/support/example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Example do
resource Example.User
resource Example.UserWithTokenRequired
resource Example.Token
resource Example.TokenWithCustomCreateTimestamp
resource Example.UserIdentity
resource Example.UserWithRegisterMagicLink
end
Expand Down
10 changes: 10 additions & 0 deletions test/support/example/token_with_custom_create_timestamp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Example.TokenWithCustomCreateTimestamp do
@moduledoc false
use Ash.Resource,
extensions: [AshAuthentication.TokenResource],
domain: Example

attributes do
create_timestamp :inserted_at
end
end

0 comments on commit 733991f

Please sign in to comment.