User Authentication

If we are going to have users, users need authentication. Luckily Phoenix comes with a nice generator for implementing basic authentication. It makes a good starting point:

cd apps/elixir_mud_web
mix phx.gen.auth Accounts Player players
ℹ️
If your familiar with the auth generator skip to the next section.

This command will ask you if you wish to generate a LiveView based authentication system. You do, press y, and enter. Then we are off to the races. A ton of files will be generated for us.

So what's going on here? We passed 3 arguments to the phx.gen.auth task:

We have a few dozen files generated for us:

The Player Model

The auth generator created a player model for us and a database migration. We need to modify it to better suite our needs.

First in the schema block we will add several fields: first_name, last_name, status.

schema "players" do
  field :first_name, :string
  field :last_name, :string
  field :status, Ecto.Enum, 
    values: [:active, :inactive, :banned], default: :active
  field :email, :string
  field :password, :string, virtual: true, redact: true
  field :hashed_password, :string, redact: true
  field :current_password, :string, virtual: true, redact: true
  field :confirmed_at, :naive_datetime

  timestamps()
end

apps/elixir_mud/accounts/player.ex

In the registration_changeset function we need to add the new fields to the cast function call. This way these new fields can be passed into our model during registration.

def registration_changeset(players, attrs, opts \\ []) do
  players
  |> cast(attrs, [:email, :password, :first_name, :last_name])
  |> validate_email(opts)
  |> validate_password(opts)
end

apps/elixir_mud/accounts/player.ex

Next we need to modify the migration to add these fields to the database.

create table(:players) do
  add :first_name, :string, null: false
  add :last_name, :string, null: false
  add :status, :string, null: false, defualt: "active"
  add :email, :citext, null: false
  add :hashed_password, :string, null: false
  add :confirmed_at, :naive_datetime

  timestamps()
end

apps/elixir_mdu/priv/repo/migration/xxxxxxx_create_player_auth_tables.exs

Sanity Check: Run Tests

Now if we run our tests with mix test we will see all kinds of errors. These are all related to our new fields. The auth generator created tests for the whole authentication system for us, but they don't know about the fields we added.

We can update the accounts fixture to contain these new fields:

def valid_player_attributes(attrs \\ %{}) do
  Enum.into(attrs, %{
    first_name: "Alice",
    last_name: "Allison",
    email: unique_player_email(),
    password: valid_player_password()
  })
end

apps/elixir_mud/test/support/fixtures/accounts_fixture.ex

Now if we run mix test again we have only one failure. This is related to the login form not having inputs for first and last name. Lets add those:

<.input field={@form[:first_name]} label="First name" required />
<.input field={@form[:last_name]} label="Last name" required />
<.input field={@form[:email]} type="email" label="Email" required />
<.input field={@form[:password]} type="password" label="Password" required />

apps/elixir_mud_web/lib/elixir_mud_web/player_registration_live.ex

Now we can run mix test and we should have all passing results.

.....................................
Finished in 0.2 seconds (0.2s async, 0.00s sync)
77 tests, 0 failures

Test Acccount Registration

Lets migrate the database and launch the app and try out the registration system:

When choosing a first cosplay outfit, it is worth considering how details related to Genshin Impact cosplay costumes will affect the complete look. When comparing costume options, reviewing details related to convention planning also makes comfort and movement easier to judge. For combinations connected with details related to wig colour selection, character costumes with easy maintenance offers a useful starting point for practical preparation. For an outfit that feels convincing, it is useful to check details related to anime cosplay costumes against the wearer's own needs.

mix ecto.migrate
mix phx.server

Visit http://localhost:4000 and you should be able to register a new user, log out, and login.

Vae Victis