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

Discord login revised #93

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 8 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# is restricted to this project.

# General application configuration
use Mix.Config
import Config

config :hammer,
backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 4, cleanup_interval_ms: 60_000 * 10]}
Expand Down Expand Up @@ -53,7 +53,13 @@ config :uro, :pow,

config :uro, :pow_assent,
user_identities_context: Uro.UserIdentities,
providers: []
providers: [
discord: [
client_id: System.get_env("DISCORD_CLIENT_ID", ""),
client_secret: System.get_env("DISCORD_CLIENT_SECRET", ""),
strategy: Assent.Strategy.Discord
]
]

config :uro, :phoenix_swagger,
swagger_files: %{
Expand Down
33 changes: 30 additions & 3 deletions lib/uro/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ defmodule Uro.Accounts.User do
timestamps()
end

def user_identity_changeset(user_or_changeset, user_identity, attrs, user_id_attrs) do
user_or_changeset
|> user_custom_changeset(attrs, true)
|> pow_assent_user_identity_changeset(user_identity, attrs, user_id_attrs)
end

@spec lock_changeset(Schema.t() | Changeset.t()) :: Changeset.t()
def lock_changeset(user_or_changeset) do
changeset = change(user_or_changeset)
Expand All @@ -66,18 +72,24 @@ defmodule Uro.Accounts.User do
end
end

@spec user_custom_changeset(Schema.t() | Changeset.t(), Map) :: Changeset.t()
def user_custom_changeset(user_or_changeset, attrs) do
@spec user_custom_changeset(Schema.t() | Changeset.t(), Map, Boolean) :: Changeset.t()
def user_custom_changeset(user_or_changeset, attrs, force_unique_username) do
user_or_changeset
|> cast(attrs, [:username, :email_notifications])
|> validate_required([:username])
|> put_display_name
|> downcase_username
|> make_username_unique(force_unique_username)
|> validate_username(:username)
|> validate_email(:email)
|> unique_constraint(:username)
end

@spec user_custom_changeset(Schema.t() | Changeset.t(), Map) :: Changeset.t()
def user_custom_changeset(user_or_changeset, attrs) do
user_custom_changeset(user_or_changeset, attrs, false)
end

@spec changeset(Schema.t() | Changeset.t(), Map) :: Changeset.t()
def changeset(user_or_changeset, attrs) do
user_or_changeset
Expand Down Expand Up @@ -105,7 +117,22 @@ defmodule Uro.Accounts.User do
put_change(changeset, :username, username |> String.downcase())
end

def downcase_username(changeset), do: changeset
defp make_username_unique(
%Ecto.Changeset{valid?: true, changes: %{username: username}} = changeset,
make_username_unique
) do
if make_username_unique do
put_change(
changeset,
:username,
username <> "_" <> UroWeb.Helpers.UsernameStringGen.generate()
)
else
changeset
end
end

defp make_username_unique(changeset, make_username_unique), do: changeset

def validate_username(changeset, field) when is_atom(field) do
validate_change(changeset, field, fn _current_field, value ->
Expand Down
3 changes: 3 additions & 0 deletions lib/uro_web/helpers/username_string_gen.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule UroWeb.Helpers.UsernameStringGen do
use(Puid, bits: 64)
end
12 changes: 12 additions & 0 deletions lib/uro_web/pow/routes.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
defmodule UroWeb.Pow.Routes do
use Pow.Phoenix.Routes
alias UroWeb.Router.Helpers, as: Routes

def session_path(conn, verb, query_params \\ []),
do: Routes.signin_path(conn, verb, query_params)

def registration_path(conn, verb, query_params \\ []) do
if verb == :edit do
Routes.profile_path(conn, verb, query_params)
else
Routes.signup_path(conn, verb, query_params)
end
end
end
6 changes: 6 additions & 0 deletions lib/uro_web/templates/registration/edit.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
</div>
<% end %>

<span>
<%= label f, :linked_services, gettext("Linked services") %>
<%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn),
do: content_tag(:div, content_tag(:span, link)) %>
</span>

<div>
<%= submit gettext("Update") %>
</div>
Expand Down
9 changes: 8 additions & 1 deletion lib/uro_web/templates/registration/new.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,12 @@
</span>
</ul>

<%= submit gettext("Register") %>
<div>
<%= submit gettext("Register") %>
</div>

<div>
<%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn),
do: content_tag(:div, content_tag(:span, link)) %>
</div>
<% end %>
5 changes: 4 additions & 1 deletion lib/uro_web/templates/session/new.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
<div>
<%= link(gettext("Reset password"), to: Routes.pow_reset_password_reset_password_path(@conn, :new)) %>
</div>
<PowAssent.Phoenix.HTML.CoreComponents.provider_links conn={@conn} />
<div>
<%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn),
do: content_tag(:div, content_tag(:span, link)) %>
</div>
<% end %>
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ defmodule Uro.MixProject do
{:waffle_ecto, "~> 0.0.10"},
{:swoosh, "~> 1.3"},
{:hammer, "~> 6.0"},
{:scrivener_ecto, "~> 2.7"}
{:scrivener_ecto, "~> 2.7"},
{:puid, "~> 1.1"}
]
end

Expand Down
Loading