From a7909e3d8b6cb224226431fab92abf56847781cf Mon Sep 17 00:00:00 2001 From: Saracen Date: Thu, 6 May 2021 15:00:51 +0100 Subject: [PATCH 1/6] Add Discord account linking. Current does not support account creation (need way to generate valid usernames) --- config/config.exs | 8 +++++++- lib/uro_web/pow/routes.ex | 10 ++++++++++ lib/uro_web/router.ex | 2 +- lib/uro_web/templates/registration/edit.html.eex | 6 ++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config/config.exs b/config/config.exs index 06a1aac..9ae4ecb 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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: %{ diff --git a/lib/uro_web/pow/routes.ex b/lib/uro_web/pow/routes.ex index 1bb8633..95521b5 100644 --- a/lib/uro_web/pow/routes.ex +++ b/lib/uro_web/pow/routes.ex @@ -1,3 +1,13 @@ 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 diff --git a/lib/uro_web/router.ex b/lib/uro_web/router.ex index 0cbf322..dd730af 100644 --- a/lib/uro_web/router.ex +++ b/lib/uro_web/router.ex @@ -246,7 +246,7 @@ defmodule UroWeb.Router do pipe_through [:browser] pow_extension_routes() - pow_assent_routes() + pow_assent_authorization_routes() end ########### diff --git a/lib/uro_web/templates/registration/edit.html.eex b/lib/uro_web/templates/registration/edit.html.eex index 8f40e08..84a5192 100644 --- a/lib/uro_web/templates/registration/edit.html.eex +++ b/lib/uro_web/templates/registration/edit.html.eex @@ -31,6 +31,12 @@ <% end %> + + <%= label f, :linked_services, gettext("Linked services") %> + <%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn), + do: content_tag(:div, content_tag(:span, link)) %> + +
<%= submit gettext("Update") %>
From 7329d37f443e4c9b5b2909fbec941fd32e5c4ab2 Mon Sep 17 00:00:00 2001 From: Saracen Date: Thu, 6 May 2021 18:20:20 +0100 Subject: [PATCH 2/6] Add support for new account creation via Discord sign-in --- lib/uro/accounts/user.ex | 28 +++++++++++++++++++--- lib/uro_web/helpers/username_string_gen.ex | 3 +++ lib/uro_web/router.ex | 2 +- mix.exs | 3 ++- 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 lib/uro_web/helpers/username_string_gen.ex diff --git a/lib/uro/accounts/user.ex b/lib/uro/accounts/user.ex index 29e83a0..5d18937 100644 --- a/lib/uro/accounts/user.ex +++ b/lib/uro/accounts/user.ex @@ -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) @@ -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 @@ -105,7 +117,17 @@ defmodule Uro.Accounts.User do put_change(changeset, :username, username |> String.downcase()) end - def downcase_username(changeset), do: changeset + defp 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 -> diff --git a/lib/uro_web/helpers/username_string_gen.ex b/lib/uro_web/helpers/username_string_gen.ex new file mode 100644 index 0000000..7408d46 --- /dev/null +++ b/lib/uro_web/helpers/username_string_gen.ex @@ -0,0 +1,3 @@ +defmodule UroWeb.Helpers.UsernameStringGen do + use(Puid, bits: 64) +end diff --git a/lib/uro_web/router.ex b/lib/uro_web/router.ex index dd730af..0cbf322 100644 --- a/lib/uro_web/router.ex +++ b/lib/uro_web/router.ex @@ -246,7 +246,7 @@ defmodule UroWeb.Router do pipe_through [:browser] pow_extension_routes() - pow_assent_authorization_routes() + pow_assent_routes() end ########### diff --git a/mix.exs b/mix.exs index f1e3874..0d09eed 100644 --- a/mix.exs +++ b/mix.exs @@ -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 From 3c7564edff7ff1b4ed4a16b2595864ff75b83223 Mon Sep 17 00:00:00 2001 From: Saracen Date: Thu, 6 May 2021 18:26:08 +0100 Subject: [PATCH 3/6] Add sign in with Discord link to registration page. --- lib/uro_web/templates/registration/new.html.eex | 9 ++++++++- lib/uro_web/templates/session/new.html.eex | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/uro_web/templates/registration/new.html.eex b/lib/uro_web/templates/registration/new.html.eex index 758c7c6..6d0e441 100644 --- a/lib/uro_web/templates/registration/new.html.eex +++ b/lib/uro_web/templates/registration/new.html.eex @@ -35,5 +35,12 @@ - <%= submit gettext("Register") %> +
+ <%= submit gettext("Register") %> +
+ +
+ <%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn), + do: content_tag(:div, content_tag(:span, link)) %> +
<% end %> diff --git a/lib/uro_web/templates/session/new.html.eex b/lib/uro_web/templates/session/new.html.eex index ad5b667..2b90612 100644 --- a/lib/uro_web/templates/session/new.html.eex +++ b/lib/uro_web/templates/session/new.html.eex @@ -21,5 +21,8 @@
<%= link(gettext("Reset password"), to: Routes.pow_reset_password_reset_password_path(@conn, :new)) %>
- +
+ <%= for link <- PowAssent.Phoenix.ViewHelpers.provider_links(@conn), + do: content_tag(:div, content_tag(:span, link)) %> +
<% end %> From 5d4057e633bbb255caf0821460252dcfeb4a85a6 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Wed, 10 Apr 2024 11:43:53 -0700 Subject: [PATCH 4/6] mix format. --- lib/uro/accounts/user.ex | 11 +++++++++-- lib/uro_web/pow/routes.ex | 4 +++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/uro/accounts/user.ex b/lib/uro/accounts/user.ex index 5d18937..4ff66bc 100644 --- a/lib/uro/accounts/user.ex +++ b/lib/uro/accounts/user.ex @@ -119,9 +119,16 @@ defmodule Uro.Accounts.User do defp downcase_username(changeset), do: changeset - defp make_username_unique(%Ecto.Changeset{valid?: true, changes: %{username: username}} = changeset, make_username_unique) do + 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()) + put_change( + changeset, + :username, + username <> "_" <> UroWeb.Helpers.UsernameStringGen.generate() + ) else changeset end diff --git a/lib/uro_web/pow/routes.ex b/lib/uro_web/pow/routes.ex index 95521b5..c3f59af 100644 --- a/lib/uro_web/pow/routes.ex +++ b/lib/uro_web/pow/routes.ex @@ -2,7 +2,9 @@ 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 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) From d273e8fad7e6d09fa9da40d700c5aa343bdeae2c Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Wed, 10 Apr 2024 12:02:55 -0700 Subject: [PATCH 5/6] Update import statement in config file --- config/config.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index 9ae4ecb..dd7ac99 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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]} From b6a27b4446cb40e8296c45ffe8feb663b54ea095 Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Wed, 10 Apr 2024 12:10:45 -0700 Subject: [PATCH 6/6] Fix typo. --- lib/uro/accounts/user.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/uro/accounts/user.ex b/lib/uro/accounts/user.ex index 4ff66bc..ea28fa4 100644 --- a/lib/uro/accounts/user.ex +++ b/lib/uro/accounts/user.ex @@ -117,8 +117,6 @@ defmodule Uro.Accounts.User do put_change(changeset, :username, username |> String.downcase()) end - defp downcase_username(changeset), do: changeset - defp make_username_unique( %Ecto.Changeset{valid?: true, changes: %{username: username}} = changeset, make_username_unique