diff --git a/lib/nulla_web/components/templates.ex b/lib/nulla_web/components/templates.ex index 3ba56db..be2365a 100644 --- a/lib/nulla_web/components/templates.ex +++ b/lib/nulla_web/components/templates.ex @@ -1,3 +1,15 @@ +defmodule NullaWeb.PageHTML do + use NullaWeb, :html + + embed_templates "templates/page/*" +end + +defmodule NullaWeb.AuthHTML do + use NullaWeb, :html + + embed_templates "templates/auth/*" +end + defmodule NullaWeb.ActorHTML do use NullaWeb, :html diff --git a/lib/nulla_web/components/templates/auth/sign_in.html.heex b/lib/nulla_web/components/templates/auth/sign_in.html.heex new file mode 100644 index 0000000..1cdbb55 --- /dev/null +++ b/lib/nulla_web/components/templates/auth/sign_in.html.heex @@ -0,0 +1,9 @@ +
+
+ + + + + +
+
diff --git a/lib/nulla_web/components/templates/auth/sign_up.html.heex b/lib/nulla_web/components/templates/auth/sign_up.html.heex new file mode 100644 index 0000000..4685573 --- /dev/null +++ b/lib/nulla_web/components/templates/auth/sign_up.html.heex @@ -0,0 +1,12 @@ +
+
+ + + + + + + + +
+
diff --git a/lib/nulla_web/components/templates/page/home.html.heex b/lib/nulla_web/components/templates/page/home.html.heex new file mode 100644 index 0000000..e69de29 diff --git a/lib/nulla_web/controllers/auth_controller.ex b/lib/nulla_web/controllers/auth_controller.ex index 3edddb1..a4df65d 100644 --- a/lib/nulla_web/controllers/auth_controller.ex +++ b/lib/nulla_web/controllers/auth_controller.ex @@ -1,12 +1,37 @@ defmodule NullaWeb.AuthController do use NullaWeb, :controller + alias Nulla.Models.User - def sign_in do + def sign_in_view(conn, _params) do + render(conn, :sign_in, layout: false) end - def sign_out do + def sign_in(conn, _params) do + conn + |> redirect(to: "/") end - def sign_up do + def sign_out(conn, _params) do + conn + |> configure_session(drop: true) + |> put_flash(:info, "You have been logged out.") + |> redirect(to: "/") + end + + def sign_up_view(conn, _params) do + render(conn, :sign_up, layout: false) + end + + def sign_up(conn, params) do + case User.create_user(params) do + {:ok, user} -> + conn + |> put_session(:user_id, user.id) + |> put_flash(:info, "You're registred!") + |> redirect(to: "/") + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "sign_up.html", changeset: changeset) + end end end diff --git a/lib/nulla_web/controllers/page_controller.ex b/lib/nulla_web/controllers/page_controller.ex new file mode 100644 index 0000000..5d3e868 --- /dev/null +++ b/lib/nulla_web/controllers/page_controller.ex @@ -0,0 +1,7 @@ +defmodule NullaWeb.PageController do + use NullaWeb, :controller + + def home(conn, _params) do + render(conn, :home, layout: false) + end +end diff --git a/lib/nulla_web/router.ex b/lib/nulla_web/router.ex index d912802..811ff54 100644 --- a/lib/nulla_web/router.ex +++ b/lib/nulla_web/router.ex @@ -17,6 +17,8 @@ defmodule NullaWeb.Router do scope "/", NullaWeb do pipe_through :browser + get "/", PageController, :home + get "/.well-known/host-meta", HostmetaController, :index get "/.well-known/webfinger", WebfingerController, :index get "/.well-known/nodeinfo", NodeinfoController, :index @@ -24,9 +26,11 @@ defmodule NullaWeb.Router do post "/inbox", InboxController, :inbox scope "/auth" do - get "/sign_in", AuthController, :sign_in - post "/sign_out", AuthController, :sign_out - get "/sign_up", AuthController, :sign_up + get "/sign_in", AuthController, :sign_in_view + post "/sign_in", AuthController, :sign_in + delete "/sign_out", AuthController, :sign_out + get "/sign_up", AuthController, :sign_up_view + post "/sign_up", AuthController, :sign_up end scope "/users/:username" do diff --git a/test/nulla_web/controllers/page_controller_test.exs b/test/nulla_web/controllers/page_controller_test.exs new file mode 100644 index 0000000..48fa534 --- /dev/null +++ b/test/nulla_web/controllers/page_controller_test.exs @@ -0,0 +1,9 @@ +defmodule NullaWeb.PageControllerTest do + use NullaWeb.ConnCase + + test "GET /", %{conn: conn} do + conn = get(conn, ~p"/") + + assert String.length(html_response(conn, 200)) > 0 + end +end