Skip to content

Commit

Permalink
feat: add registration
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirr committed Jun 15, 2024
1 parent b0c184e commit bc5edfa
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/swipex/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Swipex.User do
def register(name, password) do
conn = Bolt.Sips.conn()
id = UUID.uuid4()

Bolt.Sips.query(conn, "CREATE (u:User {id: $id, name: $name, password: $password})", %{
id: id,
name: name,
password: password
})
end
end
32 changes: 32 additions & 0 deletions lib/swipex_web/controllers/page.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule SwipexWeb.PageController do
use SwipexWeb, :controller

def index(conn, _params) do
render(conn, :index)
end

def register(conn, _params) do
render(conn, :register)
end

def do_register(conn, %{
"name" => name,
"password" => password
}) do
with true <- String.length(name) > 0,
true <- String.length(password) > 0,
{:ok, _} <- Swipex.User.register(name, password) do
conn
|> put_flash(:info, "Registered successfully!")
|> redirect(to: "/login")
else
_ -> do_register(conn, %{})
end
end

def do_register(conn, _params) do
conn
|> put_flash(:error, "Invalid name or password.")
|> redirect(to: "/register")
end
end
28 changes: 28 additions & 0 deletions lib/swipex_web/controllers/page_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule SwipexWeb.PageHTML do
use SwipexWeb, :html
import Phoenix.HTML.Tag

def index(assigns) do
~H"""
<p>Welcome to Swipex!</p>
<p>If you have a profile, you can <a class="text-blue-400" href="/login">login here</a>.</p>
<p>
If you don't have a profile, you can <a class="text-blue-400" href="/register">register here</a>.
</p>
"""
end

def register(assigns) do
~H"""
<div class="max-w-md mx-auto">
<p>Register for Swipex</p>
<form action="/register" method="post" class="flex flex-col gap-4">
<input type="text" name="name" placeholder="Name" />
<input type="password" name="password" placeholder="Password" />
<%= csrf_input_tag("/login") %>
<input type="submit" value="Register" />
</form>
</div>
"""
end
end
11 changes: 8 additions & 3 deletions lib/swipex_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule SwipexWeb.Router do
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug Plug.CSRFProtection
plug :fetch_live_flash
plug :put_root_layout, {SwipexWeb.Layouts, :root}
plug :protect_from_forgery
Expand All @@ -17,9 +18,13 @@ defmodule SwipexWeb.Router do
scope "/", SwipexWeb do
pipe_through :browser

live_session :default, on_mount: [{SwipexWeb.Profile, :current_user}] do
live "/", ProfileLive, :index
end
get "/", PageController, :index
get "/register", PageController, :register
post "/register", PageController, :do_register

# live_session :default, on_mount: [{SwipexWeb.Profile, :current_user}] do
# live "/", ProfileLive, :index
# end
end

# Other scopes may use custom stacks.
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ defmodule Swipex.MixProject do
{:telemetry_poller, "~> 1.0"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
{:bolt_sips, "~> 2.0"}
{:bolt_sips, "~> 2.0"},
{:uuid, "~> 1.1"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"telemetry_poller": {:hex, :telemetry_poller, "1.0.0", "db91bb424e07f2bb6e73926fcafbfcbcb295f0193e0a00e825e589a0a47e8453", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b3a24eafd66c3f42da30fc3ca7dda1e9d546c12250a2d60d7b81d264fbec4f6e"},
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm", "c790593b4c3b601f5dc2378baae7efaf5b3d73c4c6456ba85759905be792f2ac"},
"websock": {:hex, :websock, "0.5.1", "c496036ce95bc26d08ba086b2a827b212c67e7cabaa1c06473cd26b40ed8cf10", [:mix], [], "hexpm", "b9f785108b81cd457b06e5f5dabe5f65453d86a99118b2c0a515e1e296dc2d2c"},
"websock_adapter": {:hex, :websock_adapter, "0.5.1", "292e6c56724e3457e808e525af0e9bcfa088cc7b9c798218e78658c7f9b85066", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "8e2e1544bfde5f9d0442f9cec2f5235398b224f75c9e06b60557debf64248ec1"},
}

0 comments on commit bc5edfa

Please sign in to comment.