Skip to content

Commit ba35aac

Browse files
Merge pull request #1 from elixirschool/user-session
user auth flow
2 parents 73a3ad3 + 016bc35 commit ba35aac

File tree

19 files changed

+2240
-2159
lines changed

19 files changed

+2240
-2159
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.
100 KB
Loading

lib/pointing_party/account.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule PointingParty.Account do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
alias PointingParty.Account
5+
6+
schema "accounts" do
7+
field :username, :string
8+
end
9+
10+
def create(attrs) do
11+
changeset = changeset(%Account{}, attrs)
12+
if changeset.valid? do
13+
account = apply_changes(changeset)
14+
{:ok, account}
15+
else
16+
{:error, %{changeset | action: :insert}}
17+
end
18+
end
19+
20+
@doc false
21+
def changeset(account, attrs \\ %{}) do
22+
account
23+
|> cast(attrs, [:username])
24+
|> validate_required([:username])
25+
end
26+
end

lib/pointing_party/account/auth.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
defmodule PointingParty.Account.Auth do
2+
alias PointingParty.Account
3+
def login(params) do
4+
Account.create(params)
5+
end
6+
end

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_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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule PointingPartyWeb.CardController do
2+
use PointingPartyWeb, :controller
3+
alias PointingParty.Card
4+
5+
def index(conn, _params) do
6+
# temporary, just to get something on the page for now
7+
card = Card.get!(1)
8+
render(conn, "index.html", card: card)
9+
end
10+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule PointingPartyWeb.SessionController do
2+
use PointingPartyWeb, :controller
3+
alias PointingParty.Account.Auth
4+
alias PointingParty.Account
5+
6+
def new(conn, _params) do
7+
changeset = Account.changeset(%Account{})
8+
render(conn, "new.html", changeset: changeset)
9+
end
10+
11+
def create(conn, params) do
12+
case Auth.login(params["account"]) do
13+
{:ok, %{username: username}} ->
14+
conn
15+
|> put_session(:username, username)
16+
|> redirect(to: "/cards")
17+
|> halt()
18+
{:error, changeset} ->
19+
render(conn, "new.html", changeset: changeset)
20+
end
21+
end
22+
23+
def delete(conn, _params) do
24+
clear_session(conn)
25+
|> redirect(to: "/login") |> halt()
26+
end
27+
end

lib/pointing_party_web/plugs/auth.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule PointingPartyWeb.Plugs.Auth do
2+
import Plug.Conn
3+
import Phoenix.Controller
4+
5+
def init(default), do: default
6+
7+
def call(conn, _default) do
8+
case authenticate(conn) do
9+
nil -> conn |> redirect(to: "/login") |> halt()
10+
username -> assign(conn, :username, username)
11+
end
12+
end
13+
14+
defp authenticate(conn) do
15+
get_session(conn, :username)
16+
end
17+
end

lib/pointing_party_web/router.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ 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
2022
end
2123

24+
scope "/", PointingPartyWeb do
25+
pipe_through [:browser, PointingPartyWeb.Plugs.Auth]
26+
get "/cards", CardController, :index
27+
end
28+
2229
# Other scopes may use custom stacks.
2330
# scope "/api", PointingPartyWeb do
2431
# pipe_through :api

0 commit comments

Comments
 (0)