mobilizon/lib/web/auth/context.ex
Thomas Citharel 7c0b813dce
Fix getting remote IP behind reverse proxy
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-08-27 15:54:43 +02:00

36 lines
692 B
Elixir

defmodule Mobilizon.Web.Auth.Context do
@moduledoc """
Guardian context for Mobilizon.Web
"""
@behaviour Plug
import Plug.Conn
alias Mobilizon.Users.User
def init(opts) do
opts
end
def call(%{assigns: %{ip: _}} = conn, _opts), do: conn
def call(conn, _opts) do
set_user_and_ip_in_context(conn)
end
def set_user_and_ip_in_context(conn) do
context = %{ip: conn.remote_ip |> :inet.ntoa() |> to_string()}
context =
case Guardian.Plug.current_resource(conn) do
%User{} = user ->
Map.put(context, :current_user, user)
nil ->
context
end
put_private(conn, :absinthe, %{context: context})
end
end