Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Functional Programming
Diskuy-Backend
Commits
92d02ed7
Commit
92d02ed7
authored
Feb 14, 2021
by
Muhammad Rafif Elfazri
Browse files
add function to generate unique username if already exist
parent
e9537db8
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/diskuy_web/auth/google_auth.ex
View file @
92d02ed7
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment