I have made an authentication page following this article.
User creation works perfectly …
When creating the user I see that it can authenticate.
Then I log out of the application and try to login …
More to no avail.
The authentication page does not work, as if it did not validate the information (email/password).
Controller session:
defmodule GcallWeb.SessionController do
use GcallWeb, :controller
alias Gcall.Structure
alias Gcall.Structure.User
def new(conn, _params) do
render(conn, "new.html")
end
def create(conn, %{"session" => auth_params}) do
user = Structure.get_by_email(auth_params["email"])
case Comeonin.Bcrypt.check_pass(user, auth_params["password"]) do
{:ok, user} ->
conn
|> put_session(:current_user_id, user.id)
|> put_flash(:info, "Conectado com sucesso.")
|> redirect(to: Routes.page_path(conn, :index))
{:error, _} ->
conn
|> put_flash(:error, "Houve um problema com seu email/senha")
|> render("new.html")
end
end
def delete(conn, _params) do
conn
|> delete_session(:current_user_id)
|> put_flash(:info, "Deslogado com sucesso!")
|> redirect(to: Routes.page_path(conn, :index))
end
end
Schema table user:
alias Comeonin.Bcrypt
schema "users" do
field :dashboard, :string
field :email, :string
field :name, :string
field :password, :string
field :profile_id, :integer, default: 1
# field :validate, :boolean, default: false
# timestamps()
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:name, :password, :email, :dashboard, :profile_id])
|> validate_required([:name, :password, :email])
|> unique_constraint(:email)
|> update_change(:password, &Bcrypt.hashpwsalt/1)
end
Template:
<h1>Sign in</h1>
<%= form_for @conn, Routes.session_path(@conn, :new), [as: :session], fn f -> %>
<%= text_input f, :email, placeholder: "email" %>
<%= password_input f, :password, placeholder: "password" %>
<%= submit "Sign in" %>
<% end %>
Debug:
info] POST /login
[debug] Processing with GcallWeb.SessionController.create/2
Parameters: %{"_csrf_token" => "LiBKdjgTUTk6U0QVACo3IWA+H0NFK0QFOl0AypikB91a7ugb4gt37SqG", "_utf8" => "✓", "session" => %{"email" => "teste2@teste.com", "password" => "[FILTERED]"}}
Pipelines: [:browser]
[debug] QUERY OK source="users" db=0.5ms
SELECT u0.`id`, u0.`dashboard`, u0.`email`, u0.`name`, u0.`password`, u0.`profile_id` FROM `users` AS u0 WHERE (u0.`email` = ?) ["teste2@teste.com"]
[info] Sent 200 in 1ms
What think it might be?
And how do I solve it?