Add Fever password changing

This commit is contained in:
Shadowfacts 2019-03-31 13:19:11 -04:00
parent cf1be80746
commit bd0fe86a81
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 53 additions and 2 deletions

View File

@ -39,6 +39,13 @@ defmodule Frenzy.User do
|> put_password_hash() |> put_password_hash()
end end
def change_fever_password_changeset(user, attrs) do
user
|> cast(attrs, [:username, :fever_password])
|> validate_length(:fever_password, min: 8)
|> put_fever_token()
end
defp put_password_hash( defp put_password_hash(
%Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset
) do ) do
@ -48,9 +55,12 @@ defmodule Frenzy.User do
defp put_fever_token( defp put_fever_token(
%Ecto.Changeset{ %Ecto.Changeset{
valid?: true, valid?: true,
changes: %{username: username, fever_password: fever_password} changes: %{fever_password: fever_password}
} = changeset } = changeset
) do ) do
username = Map.get(changeset.changes, "username") || changeset.data.username
IO.inspect(username)
change(changeset, %{ change(changeset, %{
fever_auth_token: fever_auth_token:
:crypto.hash(:md5, "#{username}:#{fever_password}") |> Base.encode16(case: :lower) :crypto.hash(:md5, "#{username}:#{fever_password}") |> Base.encode16(case: :lower)

View File

@ -56,4 +56,30 @@ defmodule FrenzyWeb.AccountController do
def do_change_password(conn, _params) do def do_change_password(conn, _params) do
redirect(conn, to: Routes.account_path(Endpoint, :change_password)) redirect(conn, to: Routes.account_path(Endpoint, :change_password))
end end
def change_fever_password(conn, _params) do
render(conn, "change_fever_password.html")
end
def do_change_fever_password(conn, %{
"new_password" => new
}) do
user = conn.assigns[:user] |> Repo.preload([:approved_clients, :groups])
changeset =
User.change_fever_password_changeset(user, %{
username: user.username,
fever_password: new
})
{:ok, user} = Repo.update(changeset)
conn
|> put_flash(:info, "Fever password changed.")
|> redirect(to: Routes.account_path(Endpoint, :show))
end
def do_change_fever_password(conn, _params) do
redirect(conn, to: Routes.account_path(Endpoint, :change_fever_password))
end
end end

View File

@ -38,6 +38,8 @@ defmodule FrenzyWeb.Router do
get "/account", AccountController, :show get "/account", AccountController, :show
get "/account/change_password", AccountController, :change_password get "/account/change_password", AccountController, :change_password
post "/account/change_password", AccountController, :do_change_password post "/account/change_password", AccountController, :do_change_password
get "/account/change_fever_password", AccountController, :change_fever_password
post "/account/change_fever_password", AccountController, :do_change_fever_password
get "/", GroupController, :index get "/", GroupController, :index
resources "/groups", GroupController, except: [:edit, :update] resources "/groups", GroupController, except: [:edit, :update]

View File

@ -0,0 +1,11 @@
<h2>Change Fever Password</h2>
<%= form_tag Routes.account_path(@conn, :do_change_fever_password), method: :post do %>
<div class="form-group">
<label for="new_password">New Fever Password</label>
<input type="password" name="new_password" id="new_password" minlength="8">
</div>
<div class="form-group">
<%= submit "Change Fever Password" %>
</div>
<% end %>

View File

@ -1,4 +1,6 @@
<h1>User Settings</h1> <h1>User Settings</h1>
<h2><%= @user.username %></h2> <h2><%= @user.username %></h2>
<a href="<%= Routes.account_path(@conn, :change_password) %>" class="button">Change Password</a> <a href="<%= Routes.account_path(@conn, :change_password) %>" class="button">Change Password</a>
<a href="<%= Routes.account_path(@conn, :change_fever_password) %>" class="button">Change Fever Password</a>