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 @@ <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>Kolab Real Time Communication</title> <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>"> </head> <body> <div class="container"> <header class="header"> - <nav role="navigation"> - <ul class="nav nav-pills pull-right"> - <li><a href="/login">Login</a></li> - </ul> - </nav> + <%= if @conn.assigns[:user] do %> + <nav role="navigation"> + <ul class="nav nav-pills pull-right"> + <li><%= link gettext("Logout"), to: "/auth/logout" %></li> + </ul> + </nav> + <% 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 %> <span class="logo"></span> </header> <p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p> <p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p> <main role="main"> <%= render @view_module, @view_template, assigns %> </main> </div> <!-- /container --> <script src="<%= static_path(@conn, "/js/app.js") %>"></script> </body> </html>