NullOranje

NullOranje

I’m working on an application that uses Pow for user authentication. One of the features I need to implement is to invite users, but this is done by a non-interactive process. Users will still verify themselves via the same process. I’m a bit lost in the documentation, but from what I see, I can use PowInvitation.Ecto.Context to create a new user changeset, but the logic of inserting that into the database and sending the invitation would be left for me to implement.

Two questions:

  1. Is this a case where it makes more sense to write the entire Invitation process myself?
  2. If I do write the backend, how do I prevent the Phoenix router from exposing the [:new, :create, :show] routes? My understanding is that these would be automagically exposed via pow_extension_routes()

Marked As Solved

danschultzer

danschultzer

Pow Core Team

You can exclude PowInvitation in the Pow.Extension.Phoenix.Router macro opts and add in the routes manually:

defmodule MyAppWeb.Router do
  use MyAppWeb, :router
  use Pow.Phoenix.Router
  use Pow.Extension.Phoenix.Router,
    extensions: [PowResetPassword, PowEmailConfirmation]

  # ...

  scope "/", PowInvitation.Phoenix, as: "pow_invitation" do
    pipe_through [:browser]

    resources "/invitations", InvitationController, only: [:edit, :update]
  end

  scope "/" do
    pipe_through :browser

    pow_routes()
    pow_extension_routes()
  end

  # ...
end

To create the invited user:

PowInvitation.Ecto.Context.create(invited_by, params, config)

To send out the invitation email you need this:

defp deliver_email(conn, user, invited_by) do
  url   = Routes.pow_invitation_path(conn, :edit, [user.invitation_token]]
  email = PowInvitation.Phoenix.Mailer.invitation(conn, user, invited_by, url)

  Pow.Phoenix.Mailer.deliver(conn, email)
end

PowInvitation expects an invited_by assoc which is the user that initiated the invitation. You can make a custom invite_changeset/3 to ignore it.

Also Liked

danschultzer

danschultzer

Pow Core Team

Set up a partial template in Phoenix, and use that in both:

# pow/registration/new.html.eex

<%= render "_form.html", changeset: @changeset, action: @action %>

# pow_invitation/invitation/new.html.eex

<%= render MyAppWeb.Pow.RegistrationView, "_form.html", changeset: @changeset, action: @action %>
MyNameIsURL

MyNameIsURL

Hey Dan,

I followed your steps to create a custom invite and called PowInvitation.Ecto.Context.create(invited_by, params, config) like you suggested, then passed the user to the deliver_email function like you suggested. But user.invitation_token is the unsigned UUID. So the emails were going out with a link like /invitations/b2a4a605-29e3-41a3-b114-89231db3da0e/edit.

So I modified your deliver_email function you posted like so. Is calling The PowInvitation.Plug.sign_invitation function the best way to generate the signed token for the email. Or is there a higher level function I should be calling to sign the token and generate the user at the same time. Here is my solution below:

defp deliver_invitation_email(conn, user, invited_by) do
    token = PowInvitation.Plug.sign_invitation_token(conn, %{
          invitation_token: user.invitation_token
        })
    url = Routes.pow_invitation_invitation_path(conn, :edit, token)
    email = PowInvitation.Phoenix.Mailer.invitation(conn, user, invited_by, url)

    Pow.Phoenix.Mailer.deliver(conn, email)
  end
danschultzer

danschultzer

Pow Core Team

If you’ve set the :web_module in the Pow config, then the extensions will also use custom templates. The PowInvitation templates are generated with mix pow.extension.phoenix.gen.templates --extension PowInvitation, and you should have seen an error if they where not generated visiting the invitation endpoints. The templates will be in templates/pow_invitation.

Last Post!

joe

joe

I needed to display the invitation link in the app for admin users. This here worked perfectly. Thank you.

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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
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
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
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

ElixirCasts
Plotto is a 100% Elixir charting library with no NIFs or external dependencies. In this episode, we’ll use it to add interactive SVG char...
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
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews