From c208484fcb272fe1fb79d6b2acc168f6640c475b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Buszkiewicz?= Date: Wed, 13 Nov 2019 00:01:39 +0100 Subject: [PATCH] Add basic Pow configuration (with Pow 1.0.21) --- .gitignore | 1 + config/config.exs | 6 ++++ lib/curious_messenger/application.ex | 3 +- lib/curious_messenger/auth/user.ex | 5 +++ lib/curious_messenger_web/endpoint.ex | 4 +++ lib/curious_messenger_web/router.ex | 7 ++++ .../templates/layout/root.html.eex | 8 ++++- .../templates/pow/registration/edit.html.eex | 30 +++++++++++++++++ .../templates/pow/registration/new.html.eex | 32 +++++++++++++++++++ .../templates/pow/session/new.html.eex | 24 ++++++++++++++ .../views/pow/registration_view.ex | 3 ++ .../views/pow/session_view.ex | 3 ++ mix.exs | 3 +- mix.lock | 1 + ...20191112214037_add_pow_fields_to_users.exs | 12 +++++++ 15 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 lib/curious_messenger_web/templates/pow/registration/edit.html.eex create mode 100644 lib/curious_messenger_web/templates/pow/registration/new.html.eex create mode 100644 lib/curious_messenger_web/templates/pow/session/new.html.eex create mode 100644 lib/curious_messenger_web/views/pow/registration_view.ex create mode 100644 lib/curious_messenger_web/views/pow/session_view.ex create mode 100644 priv/repo/migrations/20191112214037_add_pow_fields_to_users.exs diff --git a/.gitignore b/.gitignore index 6ee5dca..0ebc0eb 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ npm-debug.log .elixir_ls /config/dev.secret.exs /config/test.secret.exs +/Mnesia.* diff --git a/config/config.exs b/config/config.exs index cf4a82c..6b6a9fe 100644 --- a/config/config.exs +++ b/config/config.exs @@ -28,6 +28,12 @@ config :logger, :console, # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason +# Configure Pow with user schema for authentication +config :curious_messenger, :pow, + user: CuriousMessenger.Auth.User, + repo: CuriousMessenger.Repo, + web_module: CuriousMessengerWeb + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/lib/curious_messenger/application.ex b/lib/curious_messenger/application.ex index 802cdd9..bf7b5a0 100644 --- a/lib/curious_messenger/application.ex +++ b/lib/curious_messenger/application.ex @@ -14,7 +14,8 @@ defmodule CuriousMessenger.Application do CuriousMessengerWeb.Endpoint, # Starts a worker by calling: CuriousMessenger.Worker.start_link(arg) # {CuriousMessenger.Worker, arg}, - {Phoenix.PubSub, [name: CuriousMessenger.PubSub, adapter: Phoenix.PubSub.PG2]} + {Phoenix.PubSub, [name: CuriousMessenger.PubSub, adapter: Phoenix.PubSub.PG2]}, + Pow.Store.Backend.MnesiaCache ] # See https://hexdocs.pm/elixir/Supervisor.html diff --git a/lib/curious_messenger/auth/user.ex b/lib/curious_messenger/auth/user.ex index 494a256..ee66ed3 100644 --- a/lib/curious_messenger/auth/user.ex +++ b/lib/curious_messenger/auth/user.ex @@ -1,8 +1,12 @@ defmodule CuriousMessenger.Auth.User do use Ecto.Schema + use Pow.Ecto.Schema + import Ecto.Changeset schema "auth_users" do + pow_user_fields() + field :nickname, :string timestamps() @@ -11,6 +15,7 @@ defmodule CuriousMessenger.Auth.User do @doc false def changeset(user, attrs) do user + |> pow_changeset(attrs) |> cast(attrs, [:nickname]) |> validate_required([:nickname]) |> unique_constraint(:nickname) diff --git a/lib/curious_messenger_web/endpoint.ex b/lib/curious_messenger_web/endpoint.ex index 2b4b145..fa5aeb2 100644 --- a/lib/curious_messenger_web/endpoint.ex +++ b/lib/curious_messenger_web/endpoint.ex @@ -44,5 +44,9 @@ defmodule CuriousMessengerWeb.Endpoint do key: "_curious_messenger_key", signing_salt: "zb4BgxeG" + plug Pow.Plug.Session, + otp_app: :curious_messenger, + cache_store_backend: Pow.Store.Backend.MnesiaCache + plug CuriousMessengerWeb.Router end diff --git a/lib/curious_messenger_web/router.ex b/lib/curious_messenger_web/router.ex index e334b6c..65843b7 100644 --- a/lib/curious_messenger_web/router.ex +++ b/lib/curious_messenger_web/router.ex @@ -1,5 +1,6 @@ defmodule CuriousMessengerWeb.Router do use CuriousMessengerWeb, :router + use Pow.Phoenix.Router import Phoenix.LiveView.Router pipeline :browser do @@ -16,6 +17,12 @@ defmodule CuriousMessengerWeb.Router do plug :accepts, ["json"] end + scope "/" do + pipe_through :browser + + pow_routes() + end + scope "/", CuriousMessengerWeb do pipe_through :browser diff --git a/lib/curious_messenger_web/templates/layout/root.html.eex b/lib/curious_messenger_web/templates/layout/root.html.eex index a1fbe7c..99a64d5 100644 --- a/lib/curious_messenger_web/templates/layout/root.html.eex +++ b/lib/curious_messenger_web/templates/layout/root.html.eex @@ -12,7 +12,13 @@