Skip to content

Commit 4f2196d

Browse files
user auth flow
1 parent 73a3ad3 commit 4f2196d

File tree

13 files changed

+2204
-2124
lines changed

13 files changed

+2204
-2124
lines changed

assets/package-lock.json

Lines changed: 2102 additions & 2121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/pointing_party/card.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule PointingParty.Card do
22
use Ecto.Schema
33
import Ecto.Changeset
4+
alias PointingParty.Repo
5+
alias PointingParty.Card
46

57
schema "cards" do
68
field :description, :string
@@ -15,4 +17,8 @@ defmodule PointingParty.Card do
1517
|> cast(attrs, [:title, :description])
1618
|> validate_required([:title, :description])
1719
end
20+
21+
def get!(id) do
22+
Repo.get!(Card, id)
23+
end
1824
end

lib/pointing_party/user.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule PointingParty.User do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
schema "users" do
6+
field :username, :string
7+
end
8+
9+
@doc false
10+
def changeset(user, attrs \\ %{}) do
11+
user
12+
|> cast(attrs, [:username])
13+
|> validate_required([:username])
14+
end
15+
end

lib/pointing_party_web.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ defmodule PointingPartyWeb do
4040
use Phoenix.HTML
4141

4242
import PointingPartyWeb.ErrorHelpers
43+
import PointingPartyWeb.LayoutHelpers
4344
import PointingPartyWeb.Gettext
4445
alias PointingPartyWeb.Router.Helpers, as: Routes
4546
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule PointingPartyWeb.CardController do
2+
use PointingPartyWeb, :controller
3+
plug :authenticate_user
4+
alias PointingParty.Card
5+
6+
def index(conn, _params) do
7+
# temporary, just to get something on the page for now
8+
card = Card.get!(1)
9+
render(conn, "index.html", card: card)
10+
end
11+
12+
def authenticate_user(conn, _params) do
13+
case get_session(conn, :username) do
14+
nil -> redirect(conn, to: "/login") |> halt()
15+
username -> assign(conn, :username, username)
16+
end
17+
end
18+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule PointingPartyWeb.SessionController do
2+
use PointingPartyWeb, :controller
3+
alias PointingParty.User
4+
5+
def new(conn, _params) do
6+
changeset = User.changeset(%User{})
7+
render(conn, "new.html", changeset: changeset)
8+
end
9+
10+
def create(conn, params) do
11+
changeset = User.changeset(%User{}, params["user"])
12+
if changeset.valid? do
13+
user = Ecto.Changeset.apply_changes(changeset)
14+
put_session(conn, :username, user.username)
15+
|> redirect(to: "/cards") |> halt()
16+
else
17+
changeset = %{changeset | action: :insert}
18+
render(conn, "new.html", changeset: changeset)
19+
end
20+
end
21+
22+
def delete(conn, _params) do
23+
clear_session(conn)
24+
|> redirect(to: "/login") |> halt()
25+
end
26+
end

lib/pointing_party_web/router.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ defmodule PointingPartyWeb.Router do
1515

1616
scope "/", PointingPartyWeb do
1717
pipe_through :browser
18-
18+
get "/login", SessionController, :new
19+
post "/login", SessionController, :create
20+
delete "/logout", SessionController, :delete
1921
get "/", PageController, :index
22+
get "/cards", CardController, :index
2023
end
2124

2225
# Other scopes may use custom stacks.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= @card.title %>

lib/pointing_party_web/templates/layout/app.html.eex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
<meta charset="utf-8"/>
55
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
66
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7-
<title>PointingParty · Phoenix Framework</title>
7+
<title>PointingParty</title>
88
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
99
</head>
1010
<body>
1111
<header>
1212
<section class="container">
1313
<nav role="navigation">
1414
<ul>
15-
<li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>
15+
<%= if signed_in?(@conn) do %>
16+
<li><%= link "Log Out", to: Routes.session_path(@conn, :delete), method: :delete, class: "nav-link" %></li>
17+
<%= else %>
18+
<li><%= link "Log In", to: Routes.session_path(@conn, :new), class: "nav-link" %></li>
19+
<%= end %>
1620
</ul>
1721
</nav>
1822
<a href="http://phoenixframework.org/" class="phx-logo">
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Sign In</h1>
2+
<%= form_for @changeset, Routes.session_path(@conn, :create), fn f -> %>
3+
<label>
4+
Username: <%= text_input f, :username %>
5+
</label>
6+
<%= error_tag f, :username %>
7+
8+
<%= submit "Sign In" %>
9+
<% end %>

0 commit comments

Comments
 (0)