mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-14 05:04:49 +00:00
50 lines
1.2 KiB
Elixir
50 lines
1.2 KiB
Elixir
# Pleroma: A lightweight social networking server
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
defmodule Pleroma.PasswordResetToken do
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
alias Pleroma.User
|
|
alias Pleroma.Repo
|
|
alias Pleroma.PasswordResetToken
|
|
|
|
schema "password_reset_tokens" do
|
|
belongs_to(:user, User, type: Pleroma.FlakeId)
|
|
field(:token, :string)
|
|
field(:used, :boolean, default: false)
|
|
|
|
timestamps()
|
|
end
|
|
|
|
def create_token(%User{} = user) do
|
|
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64()
|
|
|
|
token = %PasswordResetToken{
|
|
user_id: user.id,
|
|
used: false,
|
|
token: token
|
|
}
|
|
|
|
Repo.insert(token)
|
|
end
|
|
|
|
def used_changeset(struct) do
|
|
struct
|
|
|> cast(%{}, [])
|
|
|> put_change(:used, true)
|
|
end
|
|
|
|
def reset_password(token, data) do
|
|
with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
|
|
%User{} = user <- Repo.get(User, token.user_id),
|
|
{:ok, _user} <- User.reset_password(user, data),
|
|
{:ok, token} <- Repo.update(used_changeset(token)) do
|
|
{:ok, token}
|
|
else
|
|
_e -> {:error, token}
|
|
end
|
|
end
|
|
end
|