Copser

Copser

Hello, I’m trying to get the client IP address on login and pass it to provider_option so I can send it in the email as an information to the user, it’s a standard swoosh/postmark email workflow.

Current setup for auth_plug and login method are:

auth_plug.ex

  def call(conn, _opts) do
    uid = get_session(conn, :user_id)

    unless uid do
      conn
      |> Phoenix.Controller.redirect(to: "/login")
      |> halt
    else
      user = Example.Account.User.find!(uid)

      conn
      |> assign(:user, user)
      |> put_ip_address
    end
  end

  def put_ip_address(conn) do
    ip_address =
    List.first(get_req_header(conn, "x-forwared-for")) || "0.0.0.0"

    conn
    |> put_private(:ip_address, ip_address)
  end

user.ex

  def login(email, password) do
    user =
      User
      |> where([t], t.email == ^email and not(is_nil(t.password_hash)))
      |> Repo.one

    has_valid_password = Bcrypt.verify_pass(password, (if user, do: user.password_hash, else: ""))

    if user && has_valid_password do

      new()
      |> Swoosh.Email.from("foo@bar.com")
      |> to(email)
      |> put_provider_option(:template_id, "123124")
      |> put_provider_option(:template_alias, "login-email")
      |> put_provider_option(:template_model, %{user: user.name, ip_address: ip_address})
      |> Example.Mailer.deliver()

      {:ok, user}
    else
      {:error, "Invalid email or password"}
    end
  en

In auth_plug I’ve created a custom function for fetching ip_address and I’m passing it into call function. How can I use put_ip_address in my login method and pass it to put_provider_option?

Showing Posts 1 to 2

tspenov

tspenov

That is not going to work like this.
auth_plug - checks if the user is logged in and it puts the IP address in conn only if user is logged in. (In that case you will never call User.login)
What you have to do is in your Session/LoginController, on create or whatever action you have to do the same thing (extract ip_address from conn) and pass it to User.login.

in LoginController

def create(conn, %{“email” => user, “password” => pass}) do
  ip_address = List.first(get_req_header(conn, "x-forwared-for")) || "0.0.0.0"
  User.login(email, password, ip_address)
  ...
Copser

Copser OP

@tspenov thanks for the suggestion, I need to work on this.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews