Fakultas Ilmu Komputer UI

Commit 92d02ed7 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

add function to generate unique username if already exist

parent e9537db8
defmodule DiskuyWeb.Auth.GoogleAuth do
alias DiskuyWeb.Auth.Guardian
alias Diskuy.Account
alias Diskuy.Utility.Utility
def get_access_token(code, conn) do
case ElixirAuthGoogle.get_token(code, conn) do
{:ok, profile} ->
......@@ -16,9 +18,31 @@ defmodule DiskuyWeb.Auth.GoogleAuth do
case Account.get_by_email(email) do
{:ok, user} ->
Guardian.create_token(user)
{:error, :not_found} ->
create_user(username, email, picture)
end
end
defp create_user(username, email, picture) do
case Account.get_by_username(username) do
{:ok, _user} ->
new_username = create_username(username)
{:ok, user} = Account.create_user(%{username: new_username, email: email, picture: picture})
Guardian.create_token(user)
{:error, :not_found} ->
{:ok, user} = Account.create_user(%{username: username, email: email, picture: picture})
Guardian.create_token(user)
end
end
defp create_username(username) do
new_username = "#{username}-#{Utility.random_string(6)}"
case Account.get_by_username(new_username) do
{:ok, _user} ->
create_username(username)
{:error, :not_found} ->
new_username
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment