Skip to content
This repository was archived by the owner on Jan 27, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions services/app/lib/hexlet_basics/user_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule HexletBasics.UserManager do
alias HexletBasics.User
import Ecto.Query, warn: false
alias HexletBasics.Repo
import HexletBasicsWeb.Gettext

def get_user!(id), do: Repo.get!(User, id)

Expand All @@ -18,12 +19,15 @@ defmodule HexletBasics.UserManager do
case Repo.one(query) do
nil ->
Bcrypt.no_user_verify()
{:error, :invalid_credentials}
{:error, gettext("There was a problem with your email/password")}
user ->
if Bcrypt.verify_pass(plain_text_password, user.encrypted_password) do
{:ok, user}
else
{:error, :invalid_credentials}
cond do
!User.active?(user) ->
{:error, gettext("You have not yet verified your email")}
Bcrypt.verify_pass(plain_text_password, user.encrypted_password) ->
{:ok, user}
true ->
{:error, gettext("There was a problem with your email/password")}
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ defmodule HexletBasicsWeb.SessionController do
|> redirect(to: Routes.page_path(conn, :index))
end

defp login_reply({:error, _reason}, conn) do
defp login_reply({:error, reason}, conn) do
conn
|> put_flash(:error, gettext("There was a problem with your email/password"))
|> put_flash(:error, reason)
|> new(%{})
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ defmodule HexletBasicsWeb.UserController do
else
{:ok, %User{state: state}} = Machinery.transition_to(user, UserStateMachine, "active")

user
|> User.state_changeset(%{state: state})
|> Repo.update()
user = user
|> User.state_changeset(%{state: state})
|> Repo.update!()

conn
|> Guardian.Plug.sign_in(user)
Expand Down
7 changes: 6 additions & 1 deletion services/app/priv/gettext/default.pot
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,16 @@ msgid "Registration confirmed! Welcome!"
msgstr ""

#, elixir-format
#: lib/hexlet_basics_web/controllers/session_controller.ex:35
#: lib/hexlet_basics/user_manager.ex:22 lib/hexlet_basics/user_manager.ex:30
msgid "There was a problem with your email/password"
msgstr ""

#, elixir-format
#: lib/hexlet_basics_web/templates/layout/app.html.slime:1
msgid "Switch language to"
msgstr ""

#, elixir-format
#: lib/hexlet_basics/user_manager.ex:26
msgid "You have not yet verified your email"
msgstr ""
7 changes: 6 additions & 1 deletion services/app/priv/gettext/en/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,16 @@ msgid "Registration confirmed! Welcome!"
msgstr ""

#, elixir-format
#: lib/hexlet_basics_web/controllers/session_controller.ex:35
#: lib/hexlet_basics/user_manager.ex:22 lib/hexlet_basics/user_manager.ex:30
msgid "There was a problem with your email/password"
msgstr ""

#, elixir-format
#: lib/hexlet_basics_web/templates/layout/app.html.slime:1
msgid "Switch language to"
msgstr ""

#, elixir-format
#: lib/hexlet_basics/user_manager.ex:26
msgid "You have not yet verified your email"
msgstr ""
7 changes: 6 additions & 1 deletion services/app/priv/gettext/ru/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,16 @@ msgid "Registration confirmed! Welcome!"
msgstr " Ваш e-mail подтвержден, и вы авторизованы. Добро пожаловать!"

#, elixir-format
#: lib/hexlet_basics_web/controllers/session_controller.ex:35
#: lib/hexlet_basics/user_manager.ex:22 lib/hexlet_basics/user_manager.ex:30
msgid "There was a problem with your email/password"
msgstr " Неправильные email/пароль"

#, elixir-format
#: lib/hexlet_basics_web/templates/layout/app.html.slime:1
msgid "Switch language to"
msgstr "Сменить язык на"

#, elixir-format
#: lib/hexlet_basics/user_manager.ex:26
msgid "You have not yet verified your email"
msgstr "Вы еще не подтвердили свой e-mail"
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ defmodule HexletBasicsWeb.SessionControllerTest do
assert redirected_to(conn) == page_path(conn, :index)
end

test "#create with not active state", %{conn: conn} do
user = insert(:user, Map.put(@create_attrs, :state, "waiting_confirmation"))
conn = post conn, session_path(conn, :create), session: @session_attrs

assert html_response(conn, 200)
end

test "#create when user doesnt have encrypted_password", %{conn: conn} do
user = insert(:user, %{email: "user@mail.ru"})
conn = post conn, session_path(conn, :create), session: @session_attrs
Expand Down
3 changes: 2 additions & 1 deletion services/app/test/support/factories/user_factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ defmodule HexletBasics.UserFactory do
def user_factory do
%HexletBasics.User{
github_uid: System.unique_integer([:monotonic, :positive]),
nickname: Faker.Internet.slug
nickname: Faker.Internet.slug,
state: "active"
}
end
end
Expand Down