Authentication error

def user_fixture(attrs \\ %{}) do
email = Map.get(attrs, :email, "user-#{System.unique_integer()}@example.com")

{:ok, user} =
case "password-#{System.unique_integer()}" do
password ->
Accounts.User
|> Ash.Changeset.for_create(:register_with_password, %{
email: email,
# shop_id: shop.id,
# first_name: Map.get(attrs, :first_name, "Test"),
# last_name: Map.get(attrs, :last_name, "User"),
password: password,
password_confirmation: password
})
|> Ash.create()
end

user
end
def user_fixture(attrs \\ %{}) do
email = Map.get(attrs, :email, "user-#{System.unique_integer()}@example.com")

{:ok, user} =
case "password-#{System.unique_integer()}" do
password ->
Accounts.User
|> Ash.Changeset.for_create(:register_with_password, %{
email: email,
# shop_id: shop.id,
# first_name: Map.get(attrs, :first_name, "Test"),
# last_name: Map.get(attrs, :last_name, "User"),
password: password,
password_confirmation: password
})
|> Ash.create()
end

user
end
Gives this error (attached)
Solution:
Like usually i would set current_user: user on my conn but it appears ash is doing something different
Jump to solution
6 Replies
Gonzalo Muñoz
Gonzalo Muñoz2mo ago
Yeah you're missing a case. This code only accounts for the happy path, but the moment someone fails to authenticate there's nothing to handle that.
theron
theronOP2mo ago
I replaced it with this:
def register_and_log_in_user(%{conn: conn} = context) do
email = "user@example.com"
password = "password"
{:ok, hashed_password} = AshAuthentication.BcryptProvider.hash(password)

Ash.Seed.seed!(MyApp.Accounts.User, %{
email: email,
hashed_password: hashed_password
})

# Replace `:password` with the appropriate strategy for your application.
strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :password)

{:ok, user} =
AshAuthentication.Strategy.action(strategy, :sign_in, %{
email: email,
password: password
})

new_conn =
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> AshAuthentication.Plug.Helpers.store_in_session(user)

%{context | conn: new_conn}
end
def register_and_log_in_user(%{conn: conn} = context) do
email = "user@example.com"
password = "password"
{:ok, hashed_password} = AshAuthentication.BcryptProvider.hash(password)

Ash.Seed.seed!(MyApp.Accounts.User, %{
email: email,
hashed_password: hashed_password
})

# Replace `:password` with the appropriate strategy for your application.
strategy = AshAuthentication.Info.strategy!(MyApp.Accounts.User, :password)

{:ok, user} =
AshAuthentication.Strategy.action(strategy, :sign_in, %{
email: email,
password: password
})

new_conn =
conn
|> Phoenix.ConnTest.init_test_session(%{})
|> AshAuthentication.Plug.Helpers.store_in_session(user)

%{context | conn: new_conn}
end
but idk how to pull the user off of the conn
Solution
theron
theron2mo ago
Like usually i would set current_user: user on my conn but it appears ash is doing something different
theron
theronOP2mo ago
new_conn
|> AshAuthentication.Plug.Helpers.retrieve_from_session(:coffee_viewer)
|> IO.inspect(label: "Retrieved User") # Now it's on assigns.current_user
new_conn
|> AshAuthentication.Plug.Helpers.retrieve_from_session(:coffee_viewer)
|> IO.inspect(label: "Retrieved User") # Now it's on assigns.current_user
this is not demure
Gonzalo Muñoz
Gonzalo Muñoz2mo ago
Ehm, maybe something like Map.get(socket.assigns, :current_user)? or is not a liveview? sorry, this is all mostly greek to me yet but this is how I do it
theron
theronOP2mo ago
@Gonzalo Muñoz in the test the given snippet assigns it to a session using a token. We need to call retrieve_from_session/2 to fetch the user and assign it to the conn

Did you find this page helpful?