diff --git a/web/controllers/auth_controller.ex b/web/controllers/auth_controller.ex new file mode 100644 index 0000000..611e1c3 --- /dev/null +++ b/web/controllers/auth_controller.ex @@ -0,0 +1,31 @@ +defmodule KolabChat.AuthController do + use KolabChat.Web, :controller + + @doc """ + Handler for the default logon form + """ + def default_callback(conn, params) do + %{"logon" => %{"password" => _pass, "username" => user}} = params + + cond do + is_nil(user) or user == "" -> + conn + |> put_flash(:error, gettext("Invalid username")) + |> redirect(to: "/") + true -> + conn + |> put_flash(:info, gettext("Successfully authenticated")) + |> put_session(:user, user) + |> redirect(to: "/") + end + end + + @doc """ + Handler for logout action + """ + def logout(conn, _params) do + conn + |> configure_session(drop: true) + |> redirect(to: "/") + end +end diff --git a/web/controllers/plugs/set_user.ex b/web/controllers/plugs/set_user.ex new file mode 100644 index 0000000..7b1adf8 --- /dev/null +++ b/web/controllers/plugs/set_user.ex @@ -0,0 +1,14 @@ +defmodule KolabChat.Plugs.SetUser do + import Plug.Conn + + def init(params), do: params + + def call(conn, _params) do + cond do + user = get_session(conn, :user) -> + assign(conn, :user, user) + true -> + assign(conn, :user, nil) + end + end +end diff --git a/web/router.ex b/web/router.ex index 52bcbac..b5455bd 100644 --- a/web/router.ex +++ b/web/router.ex @@ -1,26 +1,34 @@ defmodule KolabChat.Router do use KolabChat.Web, :router pipeline :browser do plug :accepts, ["html"] plug :fetch_session plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers + plug KolabChat.Plugs.SetUser end pipeline :api do plug :accepts, ["json"] end scope "/", KolabChat do pipe_through :browser # Use the default browser stack get "/", PageController, :index end + scope "/auth", KolabChat do + pipe_through :browser + + post "/default/callback", AuthController, :default_callback + get "/logout", AuthController, :logout + end + # Other scopes may use custom stacks. # scope "/api", KolabChat do # pipe_through :api # end end diff --git a/web/static/css/app.css b/web/static/css/app.css index 5314c34..b95ba2a 100644 --- a/web/static/css/app.css +++ b/web/static/css/app.css @@ -1 +1,13 @@ -/* This file is for your main application css. */ \ No newline at end of file +/* This file is for your main application css. */ + +header form { + float: right; + text-align: right; + margin-top: 15px; + margin-right: 55px; +} + +header form input { + display: block; + margin-bottom: 3px; +} diff --git a/web/templates/layout/app.html.eex b/web/templates/layout/app.html.eex index 1ca3b75..528fcbe 100644 --- a/web/templates/layout/app.html.eex +++ b/web/templates/layout/app.html.eex @@ -1,35 +1,43 @@
<%= get_flash(@conn, :info) %>
<%= get_flash(@conn, :error) %>