Skip to content

Commit

Permalink
Improve error messages on auth
Browse files Browse the repository at this point in the history
  • Loading branch information
emp7yhead committed Dec 28, 2023
1 parent 3d3d57f commit 9f8d6b2
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions services/app/apps/codebattle/lib/codebattle/oauth/firebase_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,30 @@ defmodule Codebattle.Oauth.User.FirebaseUser do
[] ->
:ok

[%User{name: ^name} | _] ->
{:error, %{name: "Nickname is already taken"}}
users ->
case Enum.find_value(users, &check_user_attributes(&1, name, email)) do
nil -> :ok
error_reason -> {:error, error_reason}
end
end
end

defp check_user_attributes(user, name, email) do
cond do
user.name == name ->
%{name: "Nickname is already taken"}

user.github_id != nil && user.email == email ->
%{email: "Email is already used. Please sign in with Github"}

user.discord_id != nil && user.email == email ->
%{email: "Email is already used. Please sign in with Discord"}

user.email == email ->
%{email: "Email is already taken"}

[%User{email: ^email} | _] ->
{:error, %{email: "Email is already taken"}}
true ->
nil
end
end

Expand Down

0 comments on commit 9f8d6b2

Please sign in to comment.