something awful login data

all the stuff to add 2 fields to the user object and to access the
logged0in user in a liveview session
This commit is contained in:
2022-07-18 15:16:57 +02:00
parent 3e8f4ef042
commit 7035fe5b2d
12 changed files with 129 additions and 62 deletions

View File

@ -95,6 +95,16 @@ defmodule SomethingErlang.Accounts do
## Settings
def change_user_sadata(%User{} = user, attrs \\ %{}) do
User.sadata_changeset(user, attrs)
end
def update_sadata(%User{} = user, attrs \\ %{}) do
user
|> change_user_sadata(attrs)
|> Repo.update()
end
@doc """
Returns an `%Ecto.Changeset{}` for changing the user email.

View File

@ -8,9 +8,20 @@ defmodule SomethingErlang.Accounts.User do
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
field :bbuserid, :string
field :bbpassword, :string
timestamps()
end
@doc """
A user changeset for SA data.
"""
def sadata_changeset(user, attrs, _opts \\ []) do
user
|> cast(attrs, [:bbuserid, :bbpassword])
end
@doc """
A user changeset for registration.

View File

@ -35,7 +35,8 @@ defmodule SomethingErlang.Forums do
** (Ecto.NoResultsError)
"""
def get_thread!(id), do: Repo.get!(Thread, id)
def get_thread!(id),
do: %Thread{id: id, thread_id: id, title: "foo"} #Repo.get!(Thread, id)
@doc """
Creates a thread.

View File

@ -4,12 +4,27 @@ defmodule SomethingErlangWeb.UserSettingsController do
alias SomethingErlang.Accounts
alias SomethingErlangWeb.UserAuth
plug :assign_email_and_password_changesets
plug :assign_changesets
def edit(conn, _params) do
render(conn, "edit.html")
end
def update(conn, %{"action" => "update_sadata"} = params) do
%{"user" => user_params} = params
user = conn.assigns.current_user
case Accounts.update_sadata(user, user_params) do
{:ok, _user} ->
conn
|> put_flash(:info, "Settings updated successfully.")
|> redirect(to: Routes.user_settings_path(conn, :edit))
{:error, changeset} ->
render(conn, "edit.html", sadata_changeset: changeset)
end
end
def update(conn, %{"action" => "update_email"} = params) do
%{"current_password" => password, "user" => user_params} = params
user = conn.assigns.current_user
@ -64,10 +79,11 @@ defmodule SomethingErlangWeb.UserSettingsController do
end
end
defp assign_email_and_password_changesets(conn, _opts) do
defp assign_changesets(conn, _opts) do
user = conn.assigns.current_user
conn
|> assign(:sadata_changeset, Accounts.change_user_sadata(user))
|> assign(:email_changeset, Accounts.change_user_email(user))
|> assign(:password_changeset, Accounts.change_user_password(user))
end

View File

@ -1,10 +1,12 @@
defmodule SomethingErlangWeb.ThreadLive.Show do
use SomethingErlangWeb, :live_view
on_mount SomethingErlangWeb.UserLiveAuth
alias SomethingErlang.Forums
@impl true
def mount(_params, _session, socket) do
socket |> IO.inspect
{:ok, socket}
end

View File

@ -0,0 +1,17 @@
defmodule SomethingErlangWeb.UserLiveAuth do
import Phoenix.LiveView
alias SomethingErlang.Accounts
def on_mount(:default, _params, %{"user_token" => user_token} = _session, socket) do
user = Accounts.get_user_by_session_token(user_token)
socket = assign_new(socket, :current_user, fn -> user end)
if socket.assigns.current_user.confirmed_at do
{:cont, socket}
else
{:halt, redirect(socket, to: "/login")}
end
end
end

View File

@ -21,13 +21,22 @@ defmodule SomethingErlangWeb.Router do
pipe_through :browser
get "/", PageController, :index
end
live "/threads", ThreadLive.Index, :index
live "/threads/new", ThreadLive.Index, :new
live "/threads/:id/edit", ThreadLive.Index, :edit
scope "/thread", SomethingErlangWeb do
pipe_through :browser
live "/threads/:id", ThreadLive.Show, :show
live "/threads/:id/show/edit", ThreadLive.Show, :edit
live "/:id", ThreadLive.Show, :show
end
scope "/admin", SomethingErlangWeb do
pipe_through [:browser, :require_authenticated_user]
live "/thread", ThreadLive.Index, :index
live "/thread/new", ThreadLive.Index, :new
live "/thread/:id/edit", ThreadLive.Index, :edit
live "/thread/:id/show/edit", ThreadLive.Show, :edit
end
# Other scopes may use custom stacks.

View File

@ -1,4 +1,4 @@
<div class="user-box flex flex-wrap">
<div class="user-box">
<%= if @current_user do %>
<h4 class="w-full"><%= @current_user.email %></h4>
<%= link "Settings", class: "link",

View File

@ -5,21 +5,27 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="csrf-token" content={csrf_token_value()}>
<%= live_title_tag assigns[:page_title] || "SomethingErlang", suffix: " · Phoenix Framework" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
<%= live_title_tag assigns[:page_title] || "SomethingErlang",
suffix: " · Phoenix Framework" %>
<link phx-track-static rel="stylesheet"
href={Routes.static_path(@conn, "/assets/app.css")}/>
<script defer phx-track-static type="text/javascript"
src={Routes.static_path(@conn, "/assets/app.js")}></script>
</head>
<body>
<header>
<section class="container mx-auto">
<nav class="flex space-around">
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
<%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %>
<nav>
<div class="navbar">
<div class="navbar-start"></div>
<div class="navbar-end">
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
<%= link "LiveDashboard",
to: Routes.live_dashboard_path(@conn, :home) %>
<% end %>
<div class="grow"></div>
<%= render "_user_menu.html", assigns %>
<%= render "_user_menu.html", assigns %>
</div>
</div>
</nav>
</section>
</header>
<%= @inner_content %>
</body>

View File

@ -1,41 +0,0 @@
<section class="phx-hero">
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<p>Peace of mind from prototype to production</p>
</section>
<section class="row">
<article class="column">
<h2>Resources</h2>
<ul>
<li>
<a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix">Source</a>
</li>
<li>
<a href="https://github.com/phoenixframework/phoenix/blob/v1.6/CHANGELOG.md">v1.6 Changelog</a>
</li>
</ul>
</article>
<article class="column">
<h2>Help</h2>
<ul>
<li>
<a href="https://elixirforum.com/c/phoenix-forum">Forum</a>
</li>
<li>
<a href="https://web.libera.chat/#elixir">#elixir on Libera Chat (IRC)</a>
</li>
<li>
<a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
</li>
<li>
<a href="https://elixir-slackin.herokuapp.com/">Elixir on Slack</a>
</li>
<li>
<a href="https://discord.gg/elixir">Elixir on Discord</a>
</li>
</ul>
</article>
</section>

View File

@ -1,5 +1,31 @@
<h1>Settings</h1>
<h3>Change SA data</h3>
<.form let={f} for={@sadata_changeset}
action={Routes.user_settings_path(@conn, :update)}
id="update_sadata">
<%= if @sadata_changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= hidden_input f, :action, name: "action", value: "update_sadata" %>
<%= label f, :bbuserid %>
<%= text_input f, :bbuserid, required: true %>
<%= error_tag f, :bbuserid %>
<%= label f, :bbpassword %>
<%= text_input f, :bbpassword, required: true %>
<%= error_tag f, :bbpassword %>
<div>
<%= submit "Change sadata", class: "btn" %>
</div>
</.form>
<h3>Change email</h3>
<.form let={f} for={@email_changeset} action={Routes.user_settings_path(@conn, :update)} id="update_email">
@ -20,7 +46,7 @@
<%= error_tag f, :current_password %>
<div>
<%= submit "Change email" %>
<%= submit "Change email", class: "btn" %>
</div>
</.form>
@ -48,6 +74,6 @@
<%= error_tag f, :current_password %>
<div>
<%= submit "Change password" %>
<%= submit "Change password", class: "btn" %>
</div>
</.form>