refactor(auth): use heex templates

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-10-07 05:58:00 +05:30
parent 2ddccce85d
commit 8f4672b516
No known key found for this signature in database
GPG Key ID: F0EA64BC1B44A7F3
13 changed files with 54 additions and 34 deletions

View File

@ -38,11 +38,13 @@ config :ketbin, KetbinWeb.Endpoint,
raise """ raise """
environment variable SWOOSH_SMTP_RELAY is missing. environment variable SWOOSH_SMTP_RELAY is missing.
""" """
username = username =
System.get_env("SWOOSH_USERNAME") || System.get_env("SWOOSH_USERNAME") ||
raise """ raise """
environment variable SWOOSH_USERNAME is missing. environment variable SWOOSH_USERNAME is missing.
""" """
password = password =
System.get_env("SWOOSH_PASSWORD") || System.get_env("SWOOSH_PASSWORD") ||
raise """ raise """
@ -58,6 +60,7 @@ config :ketbin, KetbinWeb.Endpoint,
tls: :always, tls: :always,
auth: :always, auth: :always,
port: 587 port: 587
# ## Using releases (Elixir v1.9+) # ## Using releases (Elixir v1.9+)
# #
# If you are doing OTP releases, you need to instruct Phoenix # If you are doing OTP releases, you need to instruct Phoenix

View File

@ -13,14 +13,19 @@ defmodule Ketbin.Pastes.Utils do
def generate_key(length \\ 10) do def generate_key(length \\ 10) do
random = Enum.random([0, 1]) 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 end
def is_url?(url) do def is_url?(url) do
try do try do
uri = URI.parse(url) 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 rescue
FunctionClauseError -> false FunctionClauseError -> false
end end

View File

@ -23,14 +23,23 @@ defmodule KetbinWeb.PageController do
if paste.is_url do if paste.is_url do
redirect(conn, external: paste.content) redirect(conn, external: paste.content)
else 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
end end
def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do
[head | tail] = String.split(id, ".") [head | tail] = String.split(id, ".")
paste = Pastes.get_paste!(head) 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 end
def raw(conn, %{"id" => id}) do def raw(conn, %{"id" => id}) do

View File

@ -106,6 +106,7 @@ defmodule KetbinWeb.UserAuth do
[head | _tail] = String.split(id, ".") [head | _tail] = String.split(id, ".")
paste = Pastes.get_paste!(head) paste = Pastes.get_paste!(head)
allow_edit = (user && user.id == paste.belongs_to) || false allow_edit = (user && user.id == paste.belongs_to) || false
unless allow_edit do unless allow_edit do
conn conn
|> put_flash(:error, "You don't own this paste!") |> put_flash(:error, "You don't own this paste!")

View File

@ -46,6 +46,7 @@ defmodule KetbinWeb.UserSettingsController do
|> UserAuth.log_in_user(user) |> UserAuth.log_in_user(user)
conn conn
{:error, changeset} -> {:error, changeset} ->
render(conn, "edit.html", password_changeset: changeset) render(conn, "edit.html", password_changeset: changeset)
end end

View File

@ -63,4 +63,4 @@
<%= submit "Change password" %> <%= submit "Change password" %>
</div> </div>
<% end %> <% end %>
<div> </div>

View File

@ -20,7 +20,7 @@ defmodule Ketbin.MixProject do
def application do def application do
[ [
mod: {Ketbin.Application, []}, mod: {Ketbin.Application, []},
extra_applications: [:logger, :runtime_tools, :ssl], extra_applications: [:logger, :runtime_tools, :ssl]
] ]
end end

View File

@ -75,6 +75,7 @@ defmodule KetbinWeb.PasteControllerTest do
test "deletes chosen paste", %{conn: conn, paste: paste} do test "deletes chosen paste", %{conn: conn, paste: paste} do
conn = delete(conn, Routes.paste_path(conn, :delete, paste)) conn = delete(conn, Routes.paste_path(conn, :delete, paste))
assert redirected_to(conn) == Routes.paste_path(conn, :index) assert redirected_to(conn) == Routes.paste_path(conn, :index)
assert_error_sent 404, fn -> assert_error_sent 404, fn ->
get(conn, Routes.paste_path(conn, :show, paste)) get(conn, Routes.paste_path(conn, :show, paste))
end end