Create API for Pow ResetPasword and Email veryfication extensions

i want to create json api for reset password and email confirmation for Reactjs How i handle routes , controllers, and views in this case
thanks in advance

Pow has pretty good docs. What do you find hard in doing your task? Can you be more specific?

i know pow has great work but pow only provide Login and Registeration API docs but i need Resetpassword and emailconformation exensions api docs i never found that if you have any material related to Extension Api
please provide me thanks

You can create custom controller that can call PowResetPassword.Plug.create_reset_token(user_params).
Below issue also has steps for sending mail-

1 Like

Both those extensions have links to their API docs in Pow’s README.

Sir, I repeat i was asking for API, not for phoenix template your docs is for only phoenix template that is return html templates i need json in retun data both resetpassword and emailconfirmation like this Pow api for json like this

The full custom controller for Pow ResetPasword and Email verification extensions
Email verification Controller

defmodule AlivaWeb.ConfirmEmailController do
  use AlivaWeb, :controller

  def index(conn, %{"email_confirmation_token" => token}) do
    user_token =
      conn
      |> PowEmailConfirmation.Plug.sign_confirmation_token(%{email_confirmation_token: token})

    conn
    |> PowEmailConfirmation.Plug.load_user_by_token(user_token)
    |> case do
      {:ok, conn} -> PowEmailConfirmation.Plug.confirm_email(conn, %{})
      {:error, conn} -> json(conn, %{error: "User not found"})
    end

    json(conn, %{status: "ok"})
  end
end

Pow ResetPasword Controller

defmodule AlivaWeb.PasswordResetController do
  use AlivaWeb, :controller

  def index(conn, %{"user" => %{"email" => userEmail}}) do
    conn
    |> PowResetPassword.Plug.create_reset_token(%{"email" => userEmail})
    |> case do
      {:ok, %{token: token, user: user}, conn} ->
        resetPasswordUrl = Routes.pow_reset_password_reset_password_url(conn, :edit, token)
        resetPasswordUrl = resetPasswordUrl <> "/newPassword"
        email = PowResetPassword.Phoenix.Mailer.reset_password(conn, user, resetPasswordUrl)
        Pow.Phoenix.Mailer.deliver(conn, email)
        json(conn, %{status: "Email Found"})

      {:error, _any, conn} ->
        conn
        |> json(%{error: "Email Not Fount"})
    end

    json(conn, %{userEmail: userEmail})
  end

  def reset(conn, %{"id" => token, "user" => userPasswords}) do
    conn
    |> PowResetPassword.Plug.load_user_by_token(token)
    |> case do
      {:ok, conn} ->
        PowResetPassword.Plug.update_user_password(conn, userPasswords)

      {:error, conn} ->
        json(conn, %{error: %{message: "Expired Token"}})
    end

    json(conn, %{status: "ok"})
  end
end

3 Likes