sezaru

sezaru

AshAuthentication not storing authenticated user in connection

I have this plug in my router pipeline to login an user using a magic link:

defmodule CoreWeb.Router.Plugs.LoginWithMagicToken do
  alias Core.Marketplace.Accounts.User

  import Plug.Conn

  def init(opts), do: opts

  def call(%{params: %{"magic_token" => token}, assigns: %{current_user: nil}} = conn, opts) do
    case User.sign_in_with_magic_link(%{token: token}, load: :token) do
      {:ok, user} ->
        uri = Phoenix.Controller.current_path(conn)

        conn
        |> Plug.Conn.delete_session(:return_to)
        |> AshAuthentication.Plug.Helpers.store_authentication_result({:ok, user}) # |> AshAuthentication.Plug.Helpers.store_in_session(user)
        |> Phoenix.Controller.redirect(to: uri)
        |> halt()

      _ ->
        conn
    end
  end

  def call(conn, _opts), do: conn
end

Basically, if the current_user is nil and there is a "magic_token" param in the url, it will try to sign_in with the magic link and then store it in the connection.

The User.sign_in_with_magic_link works fine, it will return the user based on the token. The issue is that I can’t make the user persist in the connection.

AFAIK, the store_authentication_result or store_in_session helper functions should do the trick, but it doesn’t seem to work, it will still have current_user as nil after the redirect.

This was working before, so I’m not sure if it was an Ash Authentication upgrade that broke it or something else.

Also, I think it is important to note that this code is ran in an iframe.

First Post!

zachdaniel

zachdaniel

Creator of Ash

When you say that it worked before, which one worked before? store_authentication_result or store_in_session? store_authentication_result wouldn’t do anything that survives a redirect, it just assigns to the conn. But store_in_session would do that.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement