2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserLive.ResetPassword do
|
|
|
|
use Shift73kWeb, :live_view
|
2021-03-02 10:35:36 -05:00
|
|
|
|
2023-01-28 08:14:32 -05:00
|
|
|
import Shift73k, only: [get_app_allow_reg: 0]
|
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Accounts.User
|
2021-03-02 10:35:36 -05:00
|
|
|
|
2022-08-14 09:14:42 -04:00
|
|
|
|
2021-03-02 10:35:36 -05:00
|
|
|
@impl true
|
|
|
|
def mount(_params, session, socket) do
|
|
|
|
user = Accounts.get_user!(session["user_id"])
|
|
|
|
|
|
|
|
socket
|
|
|
|
|> assign_defaults(session)
|
|
|
|
|> assign(page_title: "Reset password")
|
|
|
|
|> assign(changeset: Accounts.change_user_password(user))
|
|
|
|
|> assign(user: user)
|
|
|
|
|> live_okreply()
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_event("validate", %{"user" => user_params}, socket) do
|
|
|
|
cs = Accounts.change_user_password(socket.assigns.user, user_params)
|
2021-03-03 16:07:59 -05:00
|
|
|
{:noreply, socket |> assign(changeset: %{cs | action: :validate})}
|
2021-03-02 10:35:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("save", %{"user" => user_params}, socket) do
|
|
|
|
case Accounts.reset_user_password(socket.assigns.user, user_params) do
|
|
|
|
{:ok, _} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
2021-03-02 16:48:00 -05:00
|
|
|
|> put_flash(:info, "Password reset successfully.")
|
2021-03-02 10:35:36 -05:00
|
|
|
|> redirect(to: Routes.user_session_path(socket, :new))}
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> put_flash(:error, "Please check the errors below.")
|
2021-03-03 16:07:59 -05:00
|
|
|
|> assign(changeset: changeset)}
|
2021-03-02 10:35:36 -05:00
|
|
|
end
|
|
|
|
end
|
2022-08-14 09:14:42 -04:00
|
|
|
|
2023-01-28 08:14:32 -05:00
|
|
|
def allow_registration, do: get_app_allow_reg()
|
2021-03-02 10:35:36 -05:00
|
|
|
end
|