2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserResetPasswordController do
|
|
|
|
use Shift73kWeb, :controller
|
2021-03-02 10:35:36 -05:00
|
|
|
import Phoenix.LiveView.Controller
|
2020-09-12 20:07:02 -04:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Accounts
|
2020-09-12 20:07:02 -04:00
|
|
|
|
2021-03-02 10:35:36 -05:00
|
|
|
plug(:get_user_by_reset_password_token when action in [:edit])
|
2020-09-12 20:07:02 -04:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
render(conn, "new.html")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"user" => %{"email" => email}}) do
|
|
|
|
if user = Accounts.get_user_by_email(email) do
|
|
|
|
Accounts.deliver_user_reset_password_instructions(
|
|
|
|
user,
|
|
|
|
&Routes.user_reset_password_url(conn, :edit, &1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Regardless of the outcome, show an impartial success/error message.
|
|
|
|
conn
|
|
|
|
|> put_flash(
|
|
|
|
:info,
|
2021-03-02 10:35:36 -05:00
|
|
|
"If your email is in our system, you'll receive instructions to reset your password shortly."
|
2020-09-12 20:07:02 -04:00
|
|
|
)
|
|
|
|
|> redirect(to: "/")
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
2021-03-05 19:23:32 -05:00
|
|
|
live_render(conn, Shift73kWeb.UserLive.ResetPassword)
|
2020-09-12 20:07:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
defp get_user_by_reset_password_token(conn, _opts) do
|
|
|
|
%{"token" => token} = conn.params
|
|
|
|
|
|
|
|
if user = Accounts.get_user_by_reset_password_token(token) do
|
2021-03-02 10:35:36 -05:00
|
|
|
put_session(conn, "user_id", user.id)
|
2020-09-12 20:07:02 -04:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Reset password link is invalid or it has expired.")
|
|
|
|
|> redirect(to: "/")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|