Skip to content

Commit

Permalink
fix: create mask for cpf; create handle to parse cpf string
Browse files Browse the repository at this point in the history
  • Loading branch information
douglastofoli committed Nov 11, 2023
1 parent 52f8cdd commit d77b345
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
9 changes: 8 additions & 1 deletion apps/identidades/lib/identidades/handlers/usuario_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ defmodule Identidades.Handlers.UsuarioHandler do
"""
@impl true
def fetch_usuario_by_cpf_and_password(cpf, pass) do
with {:ok, user} <- Repository.fetch_usuario_by_cpf(cpf) do
with cpf <- handle_cpf(cpf),
{:ok, user} <- Repository.fetch_usuario_by_cpf(cpf) do
if valid_password?(user, pass) do
{:ok, user}
else
Expand Down Expand Up @@ -103,6 +104,12 @@ defmodule Identidades.Handlers.UsuarioHandler do
end
end

defp handle_cpf(cpf) do
cpf
|> String.replace(~r/[.-]/, "")
|> String.trim()
end

@impl true
defdelegate list_usuario, to: Repository
end
32 changes: 17 additions & 15 deletions apps/identidades/lib/identidades/models/usuario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,28 @@ defmodule Identidades.Models.Usuario do

@primary_key {:id_publico, Database.Types.PublicId, autogenerate: true}
schema "usuario" do
field :cpf, :string
field :rg, :string
field :confirmado_em, :naive_datetime
field :hash_senha, :string, redact: true
field :senha, :string, virtual: true, redact: true
field :data_nascimento, :date
field :tipo, Ecto.Enum, values: @valid_roles
field :primeiro_nome, :string
field :sobrenome, :string
field :ativo?, :boolean, default: false

has_one :pesquisador, Pesquisador,
field(:cpf, :string)
field(:rg, :string)
field(:confirmado_em, :naive_datetime)
field(:hash_senha, :string, redact: true)
field(:senha, :string, virtual: true, redact: true)
field(:data_nascimento, :date)
field(:tipo, Ecto.Enum, values: @valid_roles)
field(:primeiro_nome, :string)
field(:sobrenome, :string)
field(:ativo?, :boolean, default: false)

has_one(:pesquisador, Pesquisador,
references: :id_publico,
foreign_key: :usuario_id
)

belongs_to :contato, Contato,
belongs_to(:contato, Contato,
foreign_key: :contato_email,
references: :email_principal,
on_replace: :update,
type: :string
)

timestamps()
end
Expand Down Expand Up @@ -85,6 +87,8 @@ defmodule Identidades.Models.Usuario do
|> maybe_hash_password(opts)
end

def user_roles, do: @valid_roles

defp maybe_hash_password(changeset, opts) do
hash_password? = Keyword.get(opts, :hash, true)
password = get_change(changeset, :senha)
Expand All @@ -98,6 +102,4 @@ defmodule Identidades.Models.Usuario do
changeset
end
end

def user_roles, do: @valid_roles
end
11 changes: 11 additions & 0 deletions apps/plataforma_digital/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ if (window.location.pathname === "/acessar") {
// Phoenix Hooks
let Hooks = {};

Hooks.CpfNumberMask = {
mounted() {
this.el.addEventListener("input", e => {
let match = this.el.value.replace(/\D/g, "").match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/)
if (match) {
this.el.value = `${match[1]}.${match[2]}.${match[3]}-${match[4]}`
}
})
}
}

Hooks.NavbarHover = {
mounted() {
const navbar = document.querySelector("#auth-navbar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule PlataformaDigital.LoginHTML do
</.text>
<fieldset class="login-fieldset">
<.text_input field={@form[:cpf]} type="text" mask="999.999.999-99" label="CPF" required />
<.text_input field={@form[:cpf]} type="text" label="CPF" required phx-hook="CpfNumberMask" />
</fieldset>
<fieldset class="login-fieldset">
Expand Down

0 comments on commit d77b345

Please sign in to comment.