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

user auth flow #1

Merged
merged 7 commits into from Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4,223 changes: 2,102 additions & 2,121 deletions assets/package-lock.json

Large diffs are not rendered by default.

Binary file added assets/static/images/pp-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions lib/pointing_party/account.ex
@@ -0,0 +1,26 @@
defmodule PointingParty.Account do
use Ecto.Schema
import Ecto.Changeset
alias PointingParty.Account

schema "accounts" do
field :username, :string
end

def create(attrs) do
changeset = changeset(%Account{}, attrs)
if changeset.valid? do
account = apply_changes(changeset)
{:ok, account}
else
{:error, %{changeset | action: :insert}}
end
end

@doc false
def changeset(account, attrs \\ %{}) do
account
|> cast(attrs, [:username])
|> validate_required([:username])
end
end
6 changes: 6 additions & 0 deletions lib/pointing_party/account/auth.ex
@@ -0,0 +1,6 @@
defmodule PointingParty.Account.Auth do
alias PointingParty.Account
def login(params) do
Account.create(params)
end
end
6 changes: 6 additions & 0 deletions lib/pointing_party/card.ex
@@ -1,6 +1,8 @@
defmodule PointingParty.Card do
use Ecto.Schema
import Ecto.Changeset
alias PointingParty.Repo
alias PointingParty.Card

schema "cards" do
field :description, :string
Expand All @@ -15,4 +17,8 @@ defmodule PointingParty.Card do
|> cast(attrs, [:title, :description])
|> validate_required([:title, :description])
end

def get!(id) do
Repo.get!(Card, id)
end
end
1 change: 1 addition & 0 deletions lib/pointing_party_web.ex
Expand Up @@ -40,6 +40,7 @@ defmodule PointingPartyWeb do
use Phoenix.HTML

import PointingPartyWeb.ErrorHelpers
import PointingPartyWeb.LayoutHelpers
import PointingPartyWeb.Gettext
alias PointingPartyWeb.Router.Helpers, as: Routes
end
Expand Down
10 changes: 10 additions & 0 deletions lib/pointing_party_web/controllers/card_controller.ex
@@ -0,0 +1,10 @@
defmodule PointingPartyWeb.CardController do
use PointingPartyWeb, :controller
alias PointingParty.Card

def index(conn, _params) do
# temporary, just to get something on the page for now
card = Card.get!(1)
render(conn, "index.html", card: card)
end
end
25 changes: 25 additions & 0 deletions lib/pointing_party_web/controllers/session_controller.ex
@@ -0,0 +1,25 @@
defmodule PointingPartyWeb.SessionController do
use PointingPartyWeb, :controller
alias PointingParty.Account.Auth
alias PointingParty.Account

def new(conn, _params) do
changeset = Account.changeset(%Account{})
render(conn, "new.html", changeset: changeset)
end

def create(conn, params) do
case Auth.login(params["account"]) do
{:ok, %{username: username}} ->
put_session(conn, :username, username)
SophieDeBenedetto marked this conversation as resolved.
Show resolved Hide resolved
|> redirect(to: "/cards") |> halt()
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end

def delete(conn, _params) do
clear_session(conn)
|> redirect(to: "/login") |> halt()
end
end
17 changes: 17 additions & 0 deletions lib/pointing_party_web/plugs/auth.ex
@@ -0,0 +1,17 @@
defmodule PointingPartyWeb.Plugs.Auth do
import Plug.Conn
import Phoenix.Controller

def init(default), do: default

def call(conn, _default) do
case authenticate(conn) do
nil -> conn |> redirect(to: "/login") |> halt()
username -> assign(conn, :username, username)
end
end

defp authenticate(conn) do
get_session(conn, :username)
end
end
9 changes: 8 additions & 1 deletion lib/pointing_party_web/router.ex
Expand Up @@ -15,10 +15,17 @@ defmodule PointingPartyWeb.Router do

scope "/", PointingPartyWeb do
pipe_through :browser

get "/login", SessionController, :new
post "/login", SessionController, :create
delete "/logout", SessionController, :delete
get "/", PageController, :index
end

scope "/", PointingPartyWeb do
pipe_through [:browser, PointingPartyWeb.Plugs.Auth]
get "/cards", CardController, :index
end

# Other scopes may use custom stacks.
# scope "/api", PointingPartyWeb do
# pipe_through :api
Expand Down
1 change: 1 addition & 0 deletions lib/pointing_party_web/templates/card/index.html.eex
@@ -0,0 +1 @@
<%= @card.title %>
13 changes: 9 additions & 4 deletions lib/pointing_party_web/templates/layout/app.html.eex
Expand Up @@ -4,19 +4,24 @@
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>PointingParty · Phoenix Framework</title>
<title>PointingParty</title>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
</head>
<body>
<header>
<section class="container">
<nav role="navigation">
<ul>
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
<%= if signed_in?(@conn) do %>
<li>Hi, <%= @conn.assigns.username %></li>
<li><%= link "Log Out", to: Routes.session_path(@conn, :delete), method: :delete, class: "nav-link" %></li>
<%= else %>
<li><%= link "Log In", to: Routes.session_path(@conn, :new), class: "nav-link" %></li>
<%= end %>
</ul>
</nav>
<a href="http://phoenixframework.org/" class="phx-logo">
<img src="<%= Routes.static_path(@conn, "/images/phoenix.png") %>" alt="Phoenix Framework Logo"/>
<a href="/" class="phx-logo">
<img src="<%= Routes.static_path(@conn, "/images/pp-logo.png") %>" alt="Phoenix Framework Logo"/>
</a>
</section>
</header>
Expand Down
35 changes: 2 additions & 33 deletions lib/pointing_party_web/templates/page/index.html.eex
@@ -1,35 +1,4 @@
<section class="phx-hero">
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<p>A productive web framework that<br/>does not compromise speed or maintainability.</p>
</section>

<section class="row">
<article class="column">
<h2>Resources</h2>
<ul>
<li>
<a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix">Source</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix/blob/v1.4/CHANGELOG.md">v1.4 Changelog</a>
</li>
</ul>
</article>
<article class="column">
<h2>Help</h2>
<ul>
<li>
<a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
</li>
<li>
<a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
</li>
<li>
<a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
</li>
</ul>
</article>
<h1>Welcome to Pointing Party!</h1>
<p>A ticket estimation tool backed by the awesome real-time power of Phoenix.</p>
</section>
9 changes: 9 additions & 0 deletions lib/pointing_party_web/templates/session/new.html.eex
@@ -0,0 +1,9 @@
<h1>Sign In</h1>
<%= form_for @changeset, Routes.session_path(@conn, :create), fn f -> %>
<label>
Username: <%= text_input f, :username %>
</label>
<%= error_tag f, :username %>

<%= submit "Sign In" %>
<% end %>
3 changes: 3 additions & 0 deletions lib/pointing_party_web/views/card_view.ex
@@ -0,0 +1,3 @@
defmodule PointingPartyWeb.CardView do
use PointingPartyWeb, :view
end
10 changes: 10 additions & 0 deletions lib/pointing_party_web/views/layout_helpers.ex
@@ -0,0 +1,10 @@
defmodule PointingPartyWeb.LayoutHelpers do
use Phoenix.HTML

@doc """
Generates tag for inlined form input errors.
"""
def signed_in?(conn) do
SophieDeBenedetto marked this conversation as resolved.
Show resolved Hide resolved
not is_nil(conn.assigns[:username])
end
end
3 changes: 3 additions & 0 deletions lib/pointing_party_web/views/session_view.ex
@@ -0,0 +1,3 @@
defmodule PointingPartyWeb.SessionView do
use PointingPartyWeb, :view
end
Binary file added priv/static/images/pp-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.