diff --git a/web/controllers/auth_controller.ex b/web/controllers/auth_controller.ex new file mode 100644 --- /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 --- /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 --- a/web/router.ex +++ b/web/router.ex @@ -7,6 +7,7 @@ plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers + plug KolabChat.Plugs.SetUser end pipeline :api do @@ -19,6 +20,13 @@ 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 diff --git a/web/static/css/app.css b/web/static/css/app.css --- 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 --- a/web/templates/layout/app.html.eex +++ b/web/templates/layout/app.html.eex @@ -14,11 +14,19 @@
- + <%= if @conn.assigns[:user] do %> + + <% else %> + <%= form_for @conn, "/auth/default/callback", [as: :logon], fn f -> %> + <%= text_input f, :username, placeholder: gettext("Username") %> + <%= password_input f, :password, placeholder: gettext("Password") %> + <%= submit gettext("Log in"), class: "btn btn-primary" %> + <% end %> + <% end %>