Skip to content

Commit

Permalink
user auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
SophieDeBenedetto committed Jul 10, 2019
1 parent 73a3ad3 commit 4f2196d
Show file tree
Hide file tree
Showing 13 changed files with 2,204 additions and 2,124 deletions.
4,223 changes: 2,102 additions & 2,121 deletions assets/package-lock.json

Large diffs are not rendered by default.

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
15 changes: 15 additions & 0 deletions lib/pointing_party/user.ex
@@ -0,0 +1,15 @@
defmodule PointingParty.User do
use Ecto.Schema
import Ecto.Changeset

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

@doc false
def changeset(user, attrs \\ %{}) do
user
|> cast(attrs, [:username])
|> validate_required([:username])
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
18 changes: 18 additions & 0 deletions lib/pointing_party_web/controllers/card_controller.ex
@@ -0,0 +1,18 @@
defmodule PointingPartyWeb.CardController do
use PointingPartyWeb, :controller
plug :authenticate_user
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

def authenticate_user(conn, _params) do
case get_session(conn, :username) do
nil -> redirect(conn, to: "/login") |> halt()
username -> assign(conn, :username, username)
end
end
end
26 changes: 26 additions & 0 deletions lib/pointing_party_web/controllers/session_controller.ex
@@ -0,0 +1,26 @@
defmodule PointingPartyWeb.SessionController do
use PointingPartyWeb, :controller
alias PointingParty.User

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

def create(conn, params) do
changeset = User.changeset(%User{}, params["user"])
if changeset.valid? do
user = Ecto.Changeset.apply_changes(changeset)
put_session(conn, :username, user.username)
|> redirect(to: "/cards") |> halt()
else
changeset = %{changeset | action: :insert}
render(conn, "new.html", changeset: changeset)
end
end

def delete(conn, _params) do
clear_session(conn)
|> redirect(to: "/login") |> halt()
end
end
5 changes: 4 additions & 1 deletion lib/pointing_party_web/router.ex
Expand Up @@ -15,8 +15,11 @@ 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
get "/cards", CardController, :index
end

# Other scopes may use custom stacks.
Expand Down
1 change: 1 addition & 0 deletions lib/pointing_party_web/templates/card/index.html.eex
@@ -0,0 +1 @@
<%= @card.title %>
8 changes: 6 additions & 2 deletions lib/pointing_party_web/templates/layout/app.html.eex
Expand Up @@ -4,15 +4,19 @@
<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><%= 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">
Expand Down
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
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

0 comments on commit 4f2196d

Please sign in to comment.