diff --git a/config/config.exs b/config/config.exs index 30cb587..b20f26a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -23,9 +23,10 @@ config :logger, :console, metadata: [:request_id] # Configure authentication provider +# Replace secret_key in prod.secret.exs config :chess, Chess.Auth.Guardian, issuer: "chess", - secret_key: System.get_env("GUARDIAN_SECRET_KEY") + secret_key: "vd2vXkrYTTFKSKmNMoS2/Hk4Fxn8BkyzsVArRkxJazdQ3mr6bI4YgAC6f8ODiWlM" # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. diff --git a/lib/chess/auth/error_handler.ex b/lib/chess/auth/error_handler.ex index 988a932..eb4ccde 100644 --- a/lib/chess/auth/error_handler.ex +++ b/lib/chess/auth/error_handler.ex @@ -3,10 +3,10 @@ defmodule Chess.Auth.ErrorHandler do import Plug.Conn - def auth_error(conn, {type, _reason}, _opts) do - body = to_string(type) + def auth_error(conn, {_type, _reason}, _opts) do conn - |> put_resp_content_type("text/plain") - |> send_resp(401, body) + |> Phoenix.Controller.put_flash(:info, "You must be logged in") + |> Phoenix.Controller.redirect(to: "/") + |> halt() end end diff --git a/lib/chess/auth/guardian.ex b/lib/chess/auth/guardian.ex index db981ad..7af60b8 100644 --- a/lib/chess/auth/guardian.ex +++ b/lib/chess/auth/guardian.ex @@ -1,5 +1,5 @@ defmodule Chess.Auth.Guardian do - use Guardian, otp_app: :auth_ex + use Guardian, otp_app: :chess alias Chess.Auth @@ -10,8 +10,7 @@ defmodule Chess.Auth.Guardian do def resource_from_claims(claims) do user = claims["sub"] |> Auth.get_user! - {:ok, user} - # If something goes wrong here return {:error, reason} + {:ok, user} end end diff --git a/lib/chess_web.ex b/lib/chess_web.ex index a8a2ced..01acc79 100644 --- a/lib/chess_web.ex +++ b/lib/chess_web.ex @@ -35,7 +35,8 @@ defmodule ChessWeb do namespace: ChessWeb # Import convenience functions from controllers - import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1] + import Phoenix.Controller, + only: [get_csrf_token: 0, get_flash: 2, view_module: 1] # Use all HTML functionality (forms, tags, etc) use Phoenix.HTML diff --git a/lib/chess_web/controllers/session_controller.ex b/lib/chess_web/controllers/session_controller.ex index 27f4b38..9959837 100644 --- a/lib/chess_web/controllers/session_controller.ex +++ b/lib/chess_web/controllers/session_controller.ex @@ -3,6 +3,7 @@ defmodule ChessWeb.SessionController do alias Chess.Auth alias Chess.Auth.User + alias Chess.Auth.Guardian def new(conn, _params) do changeset = User.changeset(%User{}) @@ -14,8 +15,9 @@ defmodule ChessWeb.SessionController do %{"user" => %{"username" => username, "password" => password}} ) do case Auth.authenticate_user(username, password) do - {:ok, _user} -> + {:ok, user} -> conn + |> Guardian.Plug.sign_in(user) |> put_flash(:info, "You are signed in") |> redirect(to: game_path(conn, :index)) {:error, _error} -> @@ -25,4 +27,11 @@ defmodule ChessWeb.SessionController do |> render("new.html", changeset: changeset) end end + + def delete(conn, _params) do + conn + |> Guardian.Plug.sign_out() + |> put_flash(:info, "You are logged out") + |> redirect(to: page_path(conn, :index)) + end end diff --git a/lib/chess_web/router.ex b/lib/chess_web/router.ex index 614844f..1bb7ca4 100644 --- a/lib/chess_web/router.ex +++ b/lib/chess_web/router.ex @@ -5,21 +5,37 @@ defmodule ChessWeb.Router do plug :accepts, ["html"] plug :fetch_session plug :fetch_flash - # plug :protect_from_forgery + plug :protect_from_forgery plug :put_secure_browser_headers end + pipeline :auth do + plug Chess.Auth.Pipeline + end + + pipeline :ensure_auth do + plug Guardian.Plug.EnsureAuthenticated + end + pipeline :api do plug :accepts, ["json"] end scope "/", ChessWeb do - pipe_through :browser # Use the default browser stack + pipe_through [:browser, :auth] # Use the default browser stack get "/", PageController, :index - resources "/games", GameController, only: [:index, :create, :show, :delete] - resources "/session", SessionController, only: [:new, :create], singleton: true - resources "/registration", RegistrationController, only: [:new, :create], singleton: true + resources "/session", SessionController, + only: [:new, :create, :delete], singleton: true + resources "/registration", RegistrationController, + only: [:new, :create], singleton: true + end + + scope "/", ChessWeb do + pipe_through [:browser, :auth, :ensure_auth] + + resources "/games", GameController, + only: [:index, :create, :show, :delete] end # Other scopes may use custom stacks. diff --git a/lib/chess_web/templates/layout/app.html.eex b/lib/chess_web/templates/layout/app.html.eex index d9c756a..0b58593 100644 --- a/lib/chess_web/templates/layout/app.html.eex +++ b/lib/chess_web/templates/layout/app.html.eex @@ -15,9 +15,15 @@