1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-24 08:54:08 +00:00
mobilizon/lib/service/users/activation.ex

47 lines
1.3 KiB
Elixir
Raw Normal View History

defmodule Mobilizon.Service.Users.Activation do
@moduledoc false
2019-09-22 14:26:23 +00:00
alias Mobilizon.Service.Users.Tools
2019-09-07 23:49:56 +00:00
alias Mobilizon.Users
alias Mobilizon.Users.User
2019-09-17 00:45:32 +00:00
alias MobilizonWeb.Email
require Logger
@doc false
def check_confirmation_token(token) when is_binary(token) do
with %User{} = user <- Users.get_user_by_activation_token(token),
{:ok, %User{} = user} <-
Users.update_user(user, %{
"confirmed_at" => DateTime.utc_now() |> DateTime.truncate(:second),
"confirmation_sent_at" => nil,
"confirmation_token" => nil
}) do
Logger.info("User #{user.email} has been confirmed")
{:ok, user}
else
_err ->
{:error, :invalid_token}
end
end
def resend_confirmation_email(%User{} = user, locale \\ "en") do
with :ok <- Tools.we_can_send_email(user, :confirmation_sent_at),
{:ok, user} <-
Users.update_user(user, %{
"confirmation_sent_at" => DateTime.utc_now() |> DateTime.truncate(:second)
}) do
send_confirmation_email(user, locale)
Logger.info("Sent confirmation email again to #{user.email}")
{:ok, user.email}
end
end
def send_confirmation_email(%User{} = user, locale \\ "en") do
user
2019-09-17 00:45:32 +00:00
|> Email.User.confirmation_email(locale)
|> Email.Mailer.deliver_later()
end
end