[#114] Refactored password_reset (moved to TwitterAPI). Added homepage links to password reset result pages.

This commit is contained in:
Ivan Tashkinov 2018-12-13 13:17:49 +03:00
parent bfff2399ff
commit 908943352f
4 changed files with 22 additions and 8 deletions

View File

@ -1 +1,2 @@
<h2>Password reset failed</h2> <h2>Password reset failed</h2>
<h3><a href="<%= Pleroma.Web.base_url() %>">Homepage</a></h3>

View File

@ -1 +1,2 @@
<h2>Password changed!</h2> <h2>Password changed!</h2>
<h3><a href="<%= Pleroma.Web.base_url() %>">Homepage</a></h3>

View File

@ -167,6 +167,25 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
end end
def password_reset(nickname_or_email) do
with true <- is_binary(nickname_or_email),
%User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email),
{:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
user
|> Pleroma.UserEmail.password_reset_email(token_record.token)
|> Pleroma.Mailer.deliver()
else
false ->
{:error, "bad user identifier"}
%User{local: false} ->
{:error, "remote user"}
nil ->
{:error, "unknown user"}
end
end
def get_by_id_or_nickname(id_or_nickname) do def get_by_id_or_nickname(id_or_nickname) do
if !is_integer(id_or_nickname) && :error == Integer.parse(id_or_nickname) do if !is_integer(id_or_nickname) && :error == Integer.parse(id_or_nickname) do
Repo.get_by(User, nickname: id_or_nickname) Repo.get_by(User, nickname: id_or_nickname)

View File

@ -328,14 +328,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
def password_reset(conn, params) do def password_reset(conn, params) do
nickname_or_email = params["email"] || params["nickname"] nickname_or_email = params["email"] || params["nickname"]
with true <- is_binary(nickname_or_email), with {:ok, _} <- TwitterAPI.password_reset(nickname_or_email) do
%User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email) do
{:ok, token_record} = Pleroma.PasswordResetToken.create_token(user)
user
|> Pleroma.UserEmail.password_reset_email(token_record.token)
|> Pleroma.Mailer.deliver()
json_response(conn, :no_content, "") json_response(conn, :no_content, "")
end end
end end