Skip to content

Commit

Permalink
Add basic Pow configuration (with Pow 1.0.21)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanbush committed Nov 1, 2020
1 parent 46407cb commit c208484
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ npm-debug.log
.elixir_ls
/config/dev.secret.exs
/config/test.secret.exs
/Mnesia.*
6 changes: 6 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion lib/curious_messenger/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/curious_messenger/auth/user.ex
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/curious_messenger_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/curious_messenger_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule CuriousMessengerWeb.Router do
use CuriousMessengerWeb, :router
use Pow.Phoenix.Router
import Phoenix.LiveView.Router

pipeline :browser do
Expand All @@ -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

Expand Down
8 changes: 7 additions & 1 deletion lib/curious_messenger_web/templates/layout/root.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
<section class="container">
<nav role="navigation">
<ul>
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
<%= if Pow.Plug.current_user(@conn) do %>
<li><%= link "Profile", to: Routes.pow_registration_path(@conn, :edit) %></li>
<li><%= link "Sign out", to: Routes.pow_session_path(@conn, :delete), method: :delete %></li>
<% else %>
<li><%= link "Register", to: Routes.pow_registration_path(@conn, :new) %></li>
<li><%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %></li>
<% end %>
</ul>
</nav>
<a href="http://phoenixframework.org/" class="phx-logo">
Expand Down
30 changes: 30 additions & 0 deletions lib/curious_messenger_web/templates/pow/registration/edit.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h1>Edit profile</h1>

<%= form_for @changeset, @action, [as: :user], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= label f, :current_password %>
<%= password_input f, :current_password %>
<%= error_tag f, :current_password %>

<%= label f, :nickname %>
<%= text_input f, :nickname %>
<%= error_tag f, :nickname %>

<%= label f, :password %>
<%= password_input f, :password %>
<%= error_tag f, :password %>

<%= label f, :confirm_password %>
<%= password_input f, :confirm_password %>
<%= error_tag f, :confirm_password %>

<div>
<%= submit "Update" %>
</div>
<% end %>

32 changes: 32 additions & 0 deletions lib/curious_messenger_web/templates/pow/registration/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<h1>Register</h1>

<%= form_for @changeset, @action, [as: :user], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>

<%= label f, :nickname %>
<%= text_input f, :nickname %>
<%= error_tag f, :nickname %>

<%= label f, :password %>
<%= password_input f, :password %>
<%= error_tag f, :password %>

<%= label f, :confirm_password %>
<%= password_input f, :confirm_password %>
<%= error_tag f, :confirm_password %>

<div>
<%= submit "Register" %>
</div>
<% end %>


<span><%= link "Sign in", to: Routes.pow_session_path(@conn, :new) %></span>
24 changes: 24 additions & 0 deletions lib/curious_messenger_web/templates/pow/session/new.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>Sign in</h1>

<%= form_for @changeset, @action, [as: :user], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= label f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset) %>
<%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>

<%= label f, :password %>
<%= password_input f, :password %>
<%= error_tag f, :password %>

<div>
<%= submit "Sign in" %>
</div>
<% end %>


<span><%= link "Register", to: Routes.pow_registration_path(@conn, :new) %></span>
3 changes: 3 additions & 0 deletions lib/curious_messenger_web/views/pow/registration_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule CuriousMessengerWeb.Pow.RegistrationView do
use CuriousMessengerWeb, :view
end
3 changes: 3 additions & 0 deletions lib/curious_messenger_web/views/pow/session_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule CuriousMessengerWeb.Pow.SessionView do
use CuriousMessengerWeb, :view
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ defmodule CuriousMessenger.MixProject do
{:phoenix_live_view, "~> 0.14.8"},
{:gettext, "~> 0.18.2"},
{:jason, "~> 1.2.2"},
{:plug_cowboy, "~> 2.4.1"}
{:plug_cowboy, "~> 2.4.1"},
{:pow, "~> 1.0.21"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"plug_cowboy": {:hex, :plug_cowboy, "2.4.1", "779ba386c0915027f22e14a48919a9545714f849505fa15af2631a0d298abf0f", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d72113b6dff7b37a7d9b2a5b68892808e3a9a752f2bf7e503240945385b70507"},
"plug_crypto": {:hex, :plug_crypto, "1.2.0", "1cb20793aa63a6c619dd18bb33d7a3aa94818e5fd39ad357051a67f26dfa2df6", [:mix], [], "hexpm", "a48b538ae8bf381ffac344520755f3007cc10bd8e90b240af98ea29b69683fc2"},
"postgrex": {:hex, :postgrex, "0.15.7", "724410acd48abac529d0faa6c2a379fb8ae2088e31247687b16cacc0e0883372", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "88310c010ff047cecd73d5ceca1d99205e4b1ab1b9abfdab7e00f5c9d20ef8f9"},
"pow": {:hex, :pow, "1.0.21", "1008de8f70d2c31bcaf81d5b50bdfee0ec1fedd2c2a80348ff88722a9359817d", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0 and < 1.6.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, ">= 2.0.0 and <= 3.0.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, ">= 1.5.0 and < 2.0.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "979c213a2e2f756b8709b06cedb8b573b77cf979f77b50484a27692dfe72b18d"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
}
12 changes: 12 additions & 0 deletions priv/repo/migrations/20191112214037_add_pow_fields_to_users.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule CuriousMessenger.Repo.Migrations.AddPowFieldsToUsers do
use Ecto.Migration

def change do
alter table(:auth_users) do
add :email, :string, null: false
add :password_hash, :string
end

create unique_index(:auth_users, [:email])
end
end

0 comments on commit c208484

Please sign in to comment.