File tree Expand file tree Collapse file tree 6 files changed +41
-21
lines changed Expand file tree Collapse file tree 6 files changed +41
-21
lines changed Original file line number Diff line number Diff line change 1
- defmodule PointingParty.User do
1
+ defmodule PointingParty.Account do
2
2
use Ecto.Schema
3
3
import Ecto.Changeset
4
- alias PointingParty.User
4
+ alias PointingParty.Account
5
5
6
- schema "users " do
6
+ schema "accounts " do
7
7
field :username , :string
8
8
end
9
9
10
10
def create ( attrs ) do
11
- changeset = changeset ( % User { } , attrs )
11
+ changeset = changeset ( % Account { } , attrs )
12
12
if changeset . valid? do
13
- user = apply_changes ( changeset )
14
- { :ok , user }
13
+ account = apply_changes ( changeset )
14
+ { :ok , account }
15
15
else
16
16
{ :error , % { changeset | action: :insert } }
17
17
end
18
18
end
19
19
20
20
@ doc false
21
- def changeset ( user , attrs \\ % { } ) do
22
- user
21
+ def changeset ( account , attrs \\ % { } ) do
22
+ account
23
23
|> cast ( attrs , [ :username ] )
24
24
|> validate_required ( [ :username ] )
25
25
end
Original file line number Diff line number Diff line change
1
+ defmodule PointingParty.Account.Auth do
2
+ alias PointingParty.Account
3
+ def login ( params ) do
4
+ Account . create ( params )
5
+ end
6
+ end
Original file line number Diff line number Diff line change 1
1
defmodule PointingPartyWeb.CardController do
2
2
use PointingPartyWeb , :controller
3
- plug :authenticate_user
4
3
alias PointingParty.Card
5
4
6
5
def index ( conn , _params ) do
7
6
# temporary, just to get something on the page for now
8
7
card = Card . get! ( 1 )
9
8
render ( conn , "index.html" , card: card )
10
9
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
10
end
Original file line number Diff line number Diff line change 1
1
defmodule PointingPartyWeb.SessionController do
2
2
use PointingPartyWeb , :controller
3
- alias PointingParty.User
3
+ alias PointingParty.Account.Auth
4
+ alias PointingParty.Account
4
5
5
6
def new ( conn , _params ) do
6
- changeset = User . changeset ( % User { } )
7
+ changeset = Account . changeset ( % Account { } )
7
8
render ( conn , "new.html" , changeset: changeset )
8
9
end
9
10
10
11
def create ( conn , params ) do
11
- case User . create ( params [ "user " ] ) do
12
- { :ok , user } ->
13
- put_session ( conn , :username , user . username )
12
+ case Auth . login ( params [ "account " ] ) do
13
+ { :ok , account } ->
14
+ put_session ( conn , :username , account . username )
14
15
|> redirect ( to: "/cards" ) |> halt ( )
15
16
{ :error , changeset } ->
16
17
render ( conn , "new.html" , changeset: changeset )
Original file line number Diff line number Diff line change
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 -> redirect ( conn , 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
Original file line number Diff line number Diff line change @@ -19,6 +19,10 @@ defmodule PointingPartyWeb.Router do
19
19
post "/login" , SessionController , :create
20
20
delete "/logout" , SessionController , :delete
21
21
get "/" , PageController , :index
22
+ end
23
+
24
+ scope "/" , PointingPartyWeb do
25
+ pipe_through [ :browser , PointingPartyWeb.Plugs.Auth ]
22
26
get "/cards" , CardController , :index
23
27
end
24
28
You can’t perform that action at this time.
0 commit comments