AF
Ash Framework•2mo ago
Malian

Distinguished if the user was created or if they already existed with a magic link

I would like to perform some actions once the user has been created via a Magic Link, for example sending a welcome email and initializing other resources in other domains. However, I don't see how to distinguish between creation and reading in an after_action. According to my tests, the values of the parameters are identical in both cases. Any ideas?
Solution:
Here is what works for me: ``` upsert_fields [:email, :last_signed_in_at] ...
Jump to solution
8 Replies
ZachDaniel
ZachDaniel•2mo ago
Right now the only way is to compare the inserted at and updated at timestamps
Malian
MalianOP•2mo ago
Mhhh, when log in, the updated_at field does not seems to be updated
ZachDaniel
ZachDaniel•2mo ago
Ah, right you are 🤔 I mean, its hacky, but you could see if the user was "just created"
Malian
MalianOP•2mo ago
I added the "updated_at" field into the upsert_fields and it seems to work. upsert_fields [:email, :updated_at]
ZachDaniel
ZachDaniel•2mo ago
I think that means you're updating the user every time they sign in you may want to add a last_signed_in_at and set that in the sign in action
Malian
MalianOP•2mo ago
Indeed, that appears to be cleaner. I'll try that.
Solution
Malian
Malian•2mo ago
Here is what works for me:
upsert_fields [:email, :last_signed_in_at]

change set_attribute(:last_signed_in_at, &DateTime.utc_now/0)

attribute :last_signed_in_at, :utc_datetime_usec do
allow_nil? false
public? true
end
upsert_fields [:email, :last_signed_in_at]

change set_attribute(:last_signed_in_at, &DateTime.utc_now/0)

attribute :last_signed_in_at, :utc_datetime_usec do
allow_nil? false
public? true
end
In my case the only way of register is via magic link so the field can never be empty, at least at the moment. Thanks for helping me!
ZachDaniel
ZachDaniel•2mo ago
🥳

Did you find this page helpful?