Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages on auth #1704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
34 changes: 30 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,37 @@ 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.email == email ->
check_connected_email(user)

true ->
nil
end
end

defp check_connected_email(user) do
cond do
user.discord_id != nil ->
%{email: "Email is already used. Please sign in with Discord"}

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

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

Expand Down
Loading