Browse Source

holy shit, setup auth with pow in 20 minutes :D

master
Inhji Y. 2 years ago
parent
commit
6125e0d986
  1. BIN
      assets/static/images/user.jpg
  2. 7
      config/config.exs
  3. 10
      lib/dagon/users/user.ex
  4. 3
      lib/dagon_web/endpoint.ex
  5. 19
      lib/dagon_web/router.ex
  6. 21
      lib/dagon_web/templates/dashboard/index.html.eex
  7. 30
      lib/dagon_web/templates/pow/registration/edit.html.eex
  8. 28
      lib/dagon_web/templates/pow/registration/new.html.eex
  9. 35
      lib/dagon_web/templates/pow/session/new.html.eex
  10. 3
      lib/dagon_web/views/pow/registration_view.ex
  11. 3
      lib/dagon_web/views/pow/session_view.ex
  12. 1
      mix.exs
  13. 1
      mix.lock
  14. 14
      priv/repo/migrations/20191123143555_create_users.exs

BIN
assets/static/images/user.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

7
config/config.exs

@ -10,6 +10,13 @@ use Mix.Config
config :dagon,
ecto_repos: [Dagon.Repo]
# Configure Pow Auth
config :dagon, :pow,
user: Dagon.Users.User,
repo: Dagon.Repo,
extension: [PowPersistentSession],
web_module: DagonWeb
# Configures the endpoint
config :dagon, DagonWeb.Endpoint,
url: [host: "localhost"],

10
lib/dagon/users/user.ex

@ -0,0 +1,10 @@
defmodule Dagon.Users.User do
use Ecto.Schema
use Pow.Ecto.Schema
schema "users" do
pow_user_fields()
timestamps()
end
end

3
lib/dagon_web/endpoint.ex

@ -47,5 +47,8 @@ defmodule DagonWeb.Endpoint do
key: "_dagon_key",
signing_salt: "ABRnPJaN"
plug Pow.Plug.Session, otp_app: :dagon
plug PowPersistentSession.Plug.Cookie
plug DagonWeb.Router
end

19
lib/dagon_web/router.ex

@ -1,5 +1,6 @@
defmodule DagonWeb.Router do
use DagonWeb, :router
use Pow.Phoenix.Router
pipeline :browser do
plug :accepts, ["html"]
@ -9,13 +10,20 @@ defmodule DagonWeb.Router do
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
pipeline :protected do
plug Pow.Plug.RequireAuthenticated,
error_handler: Pow.Phoenix.PlugErrorHandler
end
scope "/", DagonWeb do
scope "/" do
pipe_through :browser
pow_routes()
end
scope "/", DagonWeb do
pipe_through [:browser, :protected]
get "/", PageController, :index
get "/dashboard", DashboardController, :index
@ -27,9 +35,4 @@ defmodule DagonWeb.Router do
resources "/workers", WorkerController, only: [:index, :edit, :update]
get "/workers/:id/run", WorkerController, :run
end
# Other scopes may use custom stacks.
# scope "/api", DagonWeb do
# pipe_through :api
# end
end

21
lib/dagon_web/templates/dashboard/index.html.eex

@ -1,4 +1,25 @@
<div class="columns has-margin-top">
<div class="column is-one-quarter">
<div class="card">
<div class="card-content">
<div class="media">
<figure class="media-left">
<span class="image is-64x64">
<img src='<%= Routes.static_path(@conn, "/images/user.jpg") %>' alt="">
</span>
</figure>
<div class="media-content">
<% user = Pow.Plug.current_user(@conn) %>
<strong><%= user.email %></strong>
<div><%= link "Sign out", to: Routes.pow_session_path(@conn, :delete), method: :delete %></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="columns ">
<div class="column">
<div class="card">
<div class="card-content">

30
lib/dagon_web/templates/pow/registration/edit.html.eex

@ -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, 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 %>
<%= label f, :confirm_password %>
<%= password_input f, :confirm_password %>
<%= error_tag f, :confirm_password %>
<div>
<%= submit "Update" %>
</div>
<% end %>

28
lib/dagon_web/templates/pow/registration/new.html.eex

@ -0,0 +1,28 @@
<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, :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>

35
lib/dagon_web/templates/pow/session/new.html.eex

@ -0,0 +1,35 @@
<h1>Sign in</h1>
<div class="card">
<div class="card-content">
<%= form_for @changeset, @action, [as: :user, class: "form"], fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="field">
<%= label f, Pow.Ecto.Schema.user_id_field(@changeset), class: "label" %>
<%= text_input f, Pow.Ecto.Schema.user_id_field(@changeset), class: "input" %>
<%= error_tag f, Pow.Ecto.Schema.user_id_field(@changeset) %>
</div>
<div class="field">
<%= label f, :password, class: "label" %>
<%= password_input f, :password, class: "input" %>
<%= error_tag f, :password %>
</div>
<div class="field is-grouped">
<div class="control">
<%= submit "Sign in", class: "button is-primary" %>
</div>
<div class="control">
<%= link "Register", to: Routes.pow_registration_path(@conn, :new), class: "button" %>
</div>
</div>
<% end %>
</div>
</div>

3
lib/dagon_web/views/pow/registration_view.ex

@ -0,0 +1,3 @@
defmodule DagonWeb.Pow.RegistrationView do
use DagonWeb, :view
end

3
lib/dagon_web/views/pow/session_view.ex

@ -0,0 +1,3 @@
defmodule DagonWeb.Pow.SessionView do
use DagonWeb, :view
end

1
mix.exs

@ -48,6 +48,7 @@ defmodule Dagon.MixProject do
{:plug_cowboy, "~> 2.0"},
{:postgrex, ">= 0.0.0"},
{:timex, "~> 3.6"},
{:pow, "~> 1.0.15"},
{:quantum, "~> 2.3"},
{:que, "~> 0.10.1"}
]

1
mix.lock

@ -38,6 +38,7 @@
"plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.15.1", "23ce3417de70f4c0e9e7419ad85bdabcc6860a6925fe2c6f3b1b5b1e8e47bf2f", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"pow": {:hex, :pow, "1.0.15", "9267b5c75df2d59968585c042e2a0ec6217b1959d3afd629817461f0a20e903c", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3.0 or ~> 1.4.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"},
"quantum": {:hex, :quantum, "2.3.4", "72a0e8855e2adc101459eac8454787cb74ab4169de6ca50f670e72142d4960e9", [:mix], [{:calendar, "~> 0.17", [hex: :calendar, repo: "hexpm", optional: true]}, {:crontab, "~> 1.1", [hex: :crontab, repo: "hexpm", optional: false]}, {:gen_stage, "~> 0.12", [hex: :gen_stage, repo: "hexpm", optional: false]}, {:swarm, "~> 3.3", [hex: :swarm, repo: "hexpm", optional: false]}, {:timex, "~> 3.1", [hex: :timex, repo: "hexpm", optional: true]}], "hexpm"},
"que": {:hex, :que, "0.10.1", "788ed0ec92ed69bdf9cfb29bf41a94ca6355b8d44959bd0669cf706e557ac891", [:mix], [{:ex_utils, "~> 0.1.6", [hex: :ex_utils, repo: "hexpm", optional: false]}, {:memento, "~> 0.3.0", [hex: :memento, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},

14
priv/repo/migrations/20191123143555_create_users.exs

@ -0,0 +1,14 @@
defmodule Dagon.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :email, :string, null: false
add :password_hash, :string
timestamps()
end
create unique_index(:users, [:email])
end
end
Loading…
Cancel
Save