m4hi2

m4hi2

PowEmailConfirmation.Plug.email_unconfirmed?/1 always returning false

Hello, I’ve been trying to implement Pow in my phoneix project. So far everything was working fine, till I tried to deny authorization to unconfirmed emails.

The code for confirming email is as follows:

def show(conn, %{"id" => token}) do
    case PowEmailConfirmation.Plug.load_user_by_token(conn, token) do
      {:error, conn} ->
        conn
        |> put_status(401)
        |> json(%{error: %{status: 401, message: "Invalid confirmation code"}})

      {:ok, conn} ->
        case PowEmailConfirmation.Plug.confirm_email(conn, %{}) do
          {:ok, _, conn} ->
            conn
            |> json(%{success: %{message: "Email confirmed"}})

          {:error, _, conn} ->
            conn
            |> put_status(401)
            |> json(%{error: %{status: 401, message: "Invalid confirmation code"}})
        end
    end
  end

The email confirmation is working fine as it can be seen from the following:

  #Dakpeon.Users.User<
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    address: nil,
    company_name: nil,
    completion_status: false,
    email: "test1@example.com",
    email_confirmation_token: nil,
    email_confirmed_at: ~U[2021-12-20 15:25:29Z],
    first_name: nil,
    id: 5,
    inserted_at: ~N[2021-12-20 15:23:43],
    last_name: nil,
    role: "user",
    unconfirmed_email: nil,
    updated_at: ~N[2021-12-20 15:25:29],
    wallet: #Ecto.Association.NotLoaded<association :wallet is not loaded>,
    ...
  >

But the problem is whenever I call PowEmailConfirmation.Plug.email_unconfirmed?/1 I always get false. For now, I was just trying to see the output of Plug.email_unconfirmed?/1 because my logic didn’t work and that confused me.

  def create(conn, %{"user" => user_params}) do
    conn
    |> PowEmailConfirmation.Plug.email_unconfirmed?()
    |> IO.inspect()

    conn
    |> Pow.Plug.authenticate_user(user_params)
    |> case do
      {:ok, conn} ->
        json(conn, %{data: %{access_token: conn.private.api_access_token, renewal_token: conn.private.api_renewal_token}})

      {:error, conn} ->
        conn
        |> put_status(401)
        |> json(%{error: %{status: 401, message: "Invalid email or password"}})
    end
  end

Following is the output of a test user.

[debug] Processing with DakpeonWeb.API.V1.SessionController.create/2
  Parameters: %{"user" => %{"email" => "test3@example.com", "password" => "[FILTERED]"}}
  Pipelines: [:api]
false
[debug] QUERY OK source="users" db=3.6ms idle=1476.6ms
SELECT u0."id", u0."first_name", u0."last_name", u0."address", u0."company_name", u0."role", u0."completion_status", u0."unconfirmed_email", u0."email_confirmed_at", u0."email_confirmation_token", u0."password_hash", u0."email", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."email" = $1) ["test3@example.com"]

The user details:

  #Dakpeon.Users.User<
    __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
    address: nil,
    company_name: nil,
    completion_status: false,
    email: "test3@example.com",
    email_confirmation_token: "a31b635c-90b2-4cd7-b702-8f16afcf3b60",
    email_confirmed_at: nil,
    first_name: nil,
    id: 7,
    inserted_at: ~N[2021-12-27 08:53:56],
    last_name: nil,
    role: "user",
    unconfirmed_email: nil,
    updated_at: ~N[2021-12-27 08:53:56],
    wallet: #Ecto.Association.NotLoaded<association :wallet is not loaded>,
    ...
  >

This calling the email_unconfirmed?/1 function on this data should return true, according to the source code.

also, another user whose email is confirmed: (not including the user struct, cause it’s been provided before in this post)

[debug] Processing with DakpeonWeb.API.V1.SessionController.create/2
  Parameters: %{"user" => %{"email" => "test1@example.com", "password" => "[FILTERED]"}}
  Pipelines: [:api]
false
[debug] QUERY OK source="users" db=2.2ms idle=1713.9ms
SELECT u0."id", u0."first_name", u0."last_name", u0."address", u0."company_name", u0."role", u0."completion_status", u0."unconfirmed_email", u0."email_confirmed_at", u0."email_confirmation_token", u0."password_hash", u0."email", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."email" = $1) ["test1@example.com"]
[info] Sent 200 in 971ms

I’m completely lost. why it’s always returning false, is beyond my understanding. Any suggestion will be helpful!

Thanks!

Marked As Solved

al2o3cr

al2o3cr

PowEmailConfirmation.Plug.email_unconfirmed? gets the current user from the conn, but that isn’t set until Pow.Plug.authenticate_user runs.

Last Post!

m4hi2

m4hi2

ah, thanks a lot! :+1:

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement