1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.PasswordController do
7 The module contains functions for password reset.
10 use Pleroma.Web, :controller
14 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
16 alias Pleroma.PasswordResetToken
19 alias Pleroma.Web.TwitterAPI.TwitterAPI
21 plug(Pleroma.Web.Plugs.RateLimiter, [name: :request] when action == :request)
23 @doc "POST /auth/password"
24 def request(conn, params) do
25 nickname_or_email = params["email"] || params["nickname"]
27 TwitterAPI.password_reset(nickname_or_email)
29 json_response(conn, :no_content, "")
32 def reset(conn, %{"token" => token}) do
33 with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
34 false <- PasswordResetToken.expired?(token),
35 %User{} = user <- User.get_cached_by_id(token.user_id) do
36 render(conn, "reset.html", %{
41 _e -> render(conn, "invalid_token.html")
45 def do_reset(conn, %{"data" => data}) do
46 with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
47 render(conn, "reset_success.html")
49 _e -> render(conn, "reset_failed.html")