feat: add username and display it in header

main
Moritz Böhme 2024-08-18 15:42:17 +02:00
parent a782d386ee
commit 0affc58b13
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
4 changed files with 24 additions and 2 deletions

View File

@ -3,6 +3,7 @@ defmodule Pento.Accounts.User do
import Ecto.Changeset
schema "users" do
field :username, :string
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
@ -37,9 +38,18 @@ defmodule Pento.Accounts.User do
"""
def registration_changeset(user, attrs, opts \\ []) do
user
|> cast(attrs, [:email, :password])
|> cast(attrs, [:email, :password, :username])
|> validate_email(opts)
|> validate_password(opts)
|> validate_username()
end
defp validate_username(changeset) do
changeset
|> validate_required([:username])
|> validate_length(:username, min: 4, max: 20)
|> unsafe_validate_unique(:username, Pento.Repo)
|> unique_constraint(:username)
end
defp validate_email(changeset, opts) do

View File

@ -15,7 +15,7 @@
<ul class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end">
<%= if @current_user do %>
<li class="text-[0.8125rem] leading-6 text-zinc-900">
<%= @current_user.email %>
<%= @current_user.username %>
</li>
<li>
<.link

View File

@ -31,6 +31,7 @@ defmodule PentoWeb.UserRegistrationLive do
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={@form[:username]} type="text" label="Username" required />
<.input field={@form[:email]} type="email" label="Email" required />
<.input field={@form[:password]} type="password" label="Password" required />

View File

@ -0,0 +1,11 @@
defmodule Pento.Repo.Migrations.AddUsername do
use Ecto.Migration
def change do
alter table("users") do
add :username, :string, null: false
end
create unique_index(:users, [:username])
end
end