changes in generic action

Hi this is the jist of what what to do for my specific use case I know it's not possible: but there must be someway to do this by for example: making the changes runs or the action a create etc. I'm stumped right now so any advice welcome : ) action :request_magic_link do argument :email, :ci_string do allow_nil? false end change Wildflower.Accounts.AbortIfEmailRecentlySent change Wildflower.Accounts.AbortIfUserExists run AshAuthentication.Strategy.MagicLink.Request end
9 Replies
ken-kost
ken-kost•7d ago
could you explain in words what you want to achive? AFAIS you want to send a magic link to user but only if it wasn't sent recently and user does not already exist - correct? also to note:
When registration is enabled, signing in with magic is a create action that upserts the user by email. This allows a user who does not exist to request a magic link and sign up with one action.
don't know if you enabled this, but you can use magic link for registration and login. so idg why you want the abort functionality
ajst7les
ajst7lesOP•7d ago
Hey Ken essentailly I want to only send the magic link email if hasn't been sent recently (my change AbortIfEmailRecentlySent was implementing a timeout based on the email identity) and I also want to disable magic links for signing in only allowing it for registration. def handle_event("submit", %{"user" => %{"email" => email} = params}, socket) do with :ok <- abort_if_email_recently_sent(email), {:error, _error} <- MyApp.Accounts.get_byemail(email) do case dbg( Form.submit(socket.assigns.form, params: params ) ) do {:ok, } -> this is the solution I have now but it would be nice I could run this all in the action which could be single visit to the db.
ZachDaniel
ZachDaniel•7d ago
You can't add changes to generic actions, all the logic has to live in the run function
ajst7les
ajst7lesOP•7d ago
can run mutiple generic actions via the run clause in a single action? action :request_magic_link do argument :email, :ci_string do allow_nil? false end run Wildflower.Accounts.AbortIfEmailRecentlySent run Wildflower.Accounts.AbortIfUserExists run AshAuthentication.Strategy.MagicLink.Request end I'm imaging something like this I don't want to make changes to the code which came with the library and my changes don't feel large enough to warrant a new strategry. Any work rounds you can think of would be great?
ZachDaniel
ZachDaniel•7d ago
run fn input, context ->
thing1()
thign2()
AshAuthentication.Strategy.MagicLink.Request.run(input, [], context)
end
run fn input, context ->
thing1()
thign2()
AshAuthentication.Strategy.MagicLink.Request.run(input, [], context)
end
ajst7les
ajst7lesOP•7d ago
A Thanks. Are thing1() and thing2() regular elixir functions? What would I need to return to abort that whole action and stop it progressing to the next thing/magiclink.request? Lastly, if I set the trasnaction flag would that still work here?
ZachDaniel
ZachDaniel•7d ago
Yeah that was just pseudocode you can do anything you like
run fn input, context ->
if somethign() do
AshAuthentication.Strategy.MagicLink.Request.run(input, [], context)
else
{:error, "some condition not met"}
end
end
run fn input, context ->
if somethign() do
AshAuthentication.Strategy.MagicLink.Request.run(input, [], context)
else
{:error, "some condition not met"}
end
end
and yes the transaction flag would wrap it all in a transaction
ajst7les
ajst7lesOP•7d ago
Okay awesome thanks! aha I thought it was the run macro's dsl
ZachDaniel
ZachDaniel•7d ago
Understandable 😆

Did you find this page helpful?