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

@ -33,31 +33,34 @@ config :ketbin, KetbinWeb.Endpoint,
secret_key_base: secret_key_base, secret_key_base: secret_key_base,
server: true server: true
smtp_relay = smtp_relay =
System.get_env("SWOOSH_SMTP_RELAY") || System.get_env("SWOOSH_SMTP_RELAY") ||
raise """ raise """
environment variable SWOOSH_SMTP_RELAY is missing. environment variable SWOOSH_SMTP_RELAY is missing.
""" """
username =
System.get_env("SWOOSH_USERNAME") || username =
raise """ System.get_env("SWOOSH_USERNAME") ||
environment variable SWOOSH_USERNAME is missing. raise """
""" environment variable SWOOSH_USERNAME is missing.
password = """
System.get_env("SWOOSH_PASSWORD") ||
raise """ password =
environment variable SWOOSH_PASSWORD is missing. System.get_env("SWOOSH_PASSWORD") ||
""" raise """
environment variable SWOOSH_PASSWORD is missing.
"""
# configure mailer
config :ketbin, Ketbin.Mailer,
adapter: Swoosh.Adapters.SMTP,
relay: smtp_relay,
username: username,
password: password,
tls: :always,
auth: :always,
port: 587
# configure mailer
config :ketbin, Ketbin.Mailer,
adapter: Swoosh.Adapters.SMTP,
relay: smtp_relay,
username: username,
password: password,
tls: :always,
auth: :always,
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

@ -97,15 +97,16 @@ defmodule KetbinWeb.UserAuth do
end end
def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do 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) paste = Pastes.get_paste!(head)
assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false) assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false)
end end
def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do 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) 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

@ -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"> <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 -> %> <%= 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" %> <%= 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