Ash FrameworkAF
Ash Framework7mo ago
9 replies
Joan Gavelán

AshAuthentication Testing

Need help setting up a function that mocks a logged in user in my conn.

I'm using magic link authentication, this is what I've got so far:

def log_in_user(conn, user) do
  strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :magic_link)
  {:ok, token} = AshAuthentication.Strategy.MagicLink.request_token_for(strategy, user)

  conn
  |> Phoenix.ConnTest.init_test_session(%{})
  |> Plug.Conn.put_session(:user_token, token)
end


What's the correct way to go about this? Am I placing the wrong key in the session maybe? Does Ash have any helpers for this?
Solution
def log_in_user(conn, user) do
  strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :magic_link)
  {:ok, token} = AshAuthentication.Strategy.MagicLink.request_token_for(strategy, user)
  user = Accounts.sign_in_with_magic_link!(%{token: token}, authorize?: false)

  conn
  |> Phoenix.ConnTest.init_test_session(%{})
  |> AshAuthentication.Plug.Helpers.store_in_session(user)
end
Was this page helpful?