refactor(auth): use heex templates

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
sphericalkat committed 2021-10-07 05:58:00 +05:30
1 parent 2ddccce85d
commit 8f4672b516
13 files changed
+54 -34

No files matched your search

+8 -3
View File
@@ -13,14 +13,19 @@ defmodule Ketbin.Pastes.Utils do
def generate_key(length \\ 10) do
random = Enum.random([0, 1])
Enum.map(0..length, fn i -> if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() end)
|> List.to_string
Enum.map(0..length, fn i ->
if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel()
end)
|> List.to_string()
end
def is_url?(url) do
try do
uri = URI.parse(url)
uri.scheme != nil && uri.host =~ "." && Enum.member?(["https", "http", "mailto"], uri.scheme) && !(uri.host =~ "katb.in")
uri.scheme != nil && uri.host =~ "." &&
Enum.member?(["https", "http", "mailto"], uri.scheme) && !(uri.host =~ "katb.in")
rescue
FunctionClauseError -> false
end
+11 -2
View File
@@ -23,14 +23,23 @@ defmodule KetbinWeb.PageController do
if paste.is_url do
redirect(conn, external: paste.content)
else
render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "")
render(conn, "show.html",
paste: paste,
show_edit: show_edit,
extension: List.first(tail) || ""
)
end
end
def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
[head | tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
render(conn, "show.html", paste: paste, show_edit: show_edit, extension: (if tail == [], do: "", else: tail))
render(conn, "show.html",
paste: paste,
show_edit: show_edit,
extension: if(tail == [], do: "", else: tail)
)
end
def raw(conn, %{"id" => id}) do
+3 -2
View File
@@ -97,15 +97,16 @@ defmodule KetbinWeb.UserAuth do
end
def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do
[head|_tail] = String.split(id, ".")
[head | _tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false)
end
def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do
[head|_tail] = String.split(id, ".")
[head | _tail] = String.split(id, ".")
paste = Pastes.get_paste!(head)
allow_edit = (user && user.id == paste.belongs_to) || false
unless allow_edit do
conn
|> put_flash(:error, "You don't own this paste!")
@@ -46,6 +46,7 @@ defmodule KetbinWeb.UserSettingsController do
|> UserAuth.log_in_user(user)
conn
{:error, changeset} ->
render(conn, "edit.html", password_changeset: changeset)
end
@@ -1,4 +1,4 @@
<h1 class="font-bold text-4xl text-amber pt-4 w-full text-center">Settings</h1>
<h1 class="font-bold text-4xl text-amber pt-4 w-full text-center">Settings</h1>
<div class="flex w-full h-full justify-center items-center">
<%= form_for @email_changeset, Routes.user_settings_path(@conn, :update), [class: "flex flex-col h-full justify-center items-start m-auto"], fn f -> %>
@@ -63,4 +63,4 @@
<%= submit "Change password" %>
</div>
<% end %>
<div>
</div>