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

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

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!")

View File

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

View File

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

View File

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