Skip to content

Commit

Permalink
feat: disallow duplicate names
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirr committed Jun 16, 2024
1 parent 361ae7e commit 3c40fd2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions lib/swipex/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ defmodule Swipex.User 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: Bcrypt.hash_pwd_salt(password)
})
case get_user_by_name(name) do
{:ok, _} ->
{:error, "User already exists."}

_ ->
Bolt.Sips.query(conn, "CREATE (u:User {id: $id, name: $name, password: $password})", %{
id: id,
name: name,
password: Bcrypt.hash_pwd_salt(password)
})
end
end

def login(name, password) do
Expand Down Expand Up @@ -35,6 +41,17 @@ defmodule Swipex.User do
end
end

def get_user_by_name(name) do
conn = Bolt.Sips.conn()

with {:ok, %Bolt.Sips.Response{results: [%{"u" => %{properties: user}}]}} <-
Bolt.Sips.query(conn, "MATCH (u:User {name: $name}) RETURN u", %{name: name}) do
{:ok, user}
else
_ -> {:error, "User not found."}
end
end

def delete_user_by_id(id) do
conn = Bolt.Sips.conn()

Expand Down
2 changes: 1 addition & 1 deletion lib/swipex_web/controllers/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule SwipexWeb.PageController do

def do_register(conn, _params) do
conn
|> put_flash(:error, "Invalid name or password.")
|> put_flash(:error, "Could not register. Please try again.")
|> redirect(to: "/register")
end

Expand Down

0 comments on commit 3c40fd2

Please sign in to comment.