Hacker News new | ask | show | jobs
by AlchemistCamp 2616 days ago
I've got an app with back-end oauth-based login. I had a bit of a headache integrating the sessions with Absinthe and finally arrived on this in my Context module:

```

  def call(conn, _) do
    context = build_context(conn)
    Absinthe.Plug.put_options(conn, context: context)
  end

  def before_send(conn, %Absinthe.Blueprint{} = blueprint) do
    if blueprint.execution.context[:logout?] do
      Auth.drop_current_user(conn)
    else
      conn
    end
  end

  defp build_context(conn) do
    with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
         {:ok, data} <- MyApp.Token.verify(token),
         %{} = user <- get_user(data) do
      %{current_user: user}
    else
      _ -> %{}
    end
  end

  # rest of the file
```

Then made a Logout middleware that sets logout? to true in the resolution context.

Is digging into the Blueprint as in the code above necessary? Is there a simpler way of solving this?