Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Disable email verification by default

Uku Taht2ca3baeafd37
diff --git a/config/runtime.exs b/config/runtime.exs
index c75ef813d0..72a208bac2 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -116,6 +116,11 @@ disable_auth =
|> get_var_from_path_or_env("DISABLE_AUTH", "false")
|> String.to_existing_atom()
+enable_email_verification =
+ config_dir
+ |> get_var_from_path_or_env("ENABLE_EMAIL_VERIFICATION", "false")
+ |> String.to_existing_atom()
+
disable_registration =
config_dir
|> get_var_from_path_or_env("DISABLE_REGISTRATION", "false")
@@ -184,6 +189,7 @@ config :plausible,
config :plausible, :selfhost,
disable_authentication: disable_auth,
+ enable_email_verification: enable_email_verification,
disable_registration: if(!disable_auth, do: disable_registration, else: false)
config :plausible, PlausibleWeb.Endpoint,
diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex
index 05e26ab402..a367672b0e 100644
--- a/lib/plausible/auth/user.ex
+++ b/lib/plausible/auth/user.ex
@@ -38,6 +38,7 @@ defmodule Plausible.Auth.User do
|> validate_confirmation(:password)
|> hash_password()
|> start_trial
+ |> set_email_verified
|> unique_constraint(:email)
end
@@ -84,4 +85,12 @@ defmodule Plausible.Auth.User do
Timex.today() |> Timex.shift(days: 30)
end
end
+
+ defp set_email_verified(user) do
+ if Keyword.fetch!(Application.get_env(:plausible, :selfhost), :enable_email_verification) do
+ change(user, email_verified: false)
+ else
+ change(user, email_verified: true)
+ end
+ end
end
diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex
index d70ba6cc9b..28c26aefea 100644
--- a/lib/plausible_web/controllers/auth_controller.ex
+++ b/lib/plausible_web/controllers/auth_controller.ex
@@ -41,18 +41,16 @@ defmodule PlausibleWeb.AuthController do
if PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
case Repo.insert(user) do
{:ok, user} ->
- code = Auth.issue_email_verification(user)
- Logger.info("VERIFICATION CODE: #{code}")
- email_template = PlausibleWeb.Email.activation_email(user, code)
- Plausible.Mailer.send_email(email_template)
+ conn = set_user_session(conn, user)
- conn
- |> put_session(:current_user_id, user.id)
- |> put_resp_cookie("logged_in", "true",
- http_only: false,
- max_age: 60 * 60 * 24 * 365 * 5000
- )
- |> redirect(to: "/activate")
+ case user.email_verified do
+ false ->
+ send_email_verification(user)
+ redirect(conn, to: "/activate")
+
+ true ->
+ redirect(conn, to: "/sites/new")
+ end
{:error, changeset} ->
render(conn, "register_form.html",
@@ -108,18 +106,16 @@ defmodule PlausibleWeb.AuthController do
if PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
case Repo.insert(user) do
{:ok, user} ->
- code = Auth.issue_email_verification(user)
- Logger.info("VERIFICATION CODE: #{code}")
- email_template = PlausibleWeb.Email.activation_email(user, code)
- Plausible.Mailer.send_email(email_template)
+ conn = set_user_session(conn, user)
- conn
- |> put_session(:current_user_id, user.id)
- |> put_resp_cookie("logged_in", "true",
- http_only: false,
- max_age: 60 * 60 * 24 * 365 * 5000
- )
- |> redirect(to: "/activate")
+ case user.email_verified do
+ false ->
+ send_email_verification(user)
+ redirect(conn, to: "/activate")
+
+ true ->
+ redirect(conn, to: "/sites")
+ end
{:error, changeset} ->
render(conn, "register_from_invitation_form.html",
@@ -139,6 +135,22 @@ defmodule PlausibleWeb.AuthController do
end
end
+ defp send_email_verification(user) do
+ code = Auth.issue_email_verification(user)
+ Logger.info("VERIFICATION CODE: #{code}")
+ email_template = PlausibleWeb.Email.activation_email(user, code)
+ Plausible.Mailer.send_email(email_template)
+ end
+
+ defp set_user_session(conn, user) do
+ conn
+ |> put_session(:current_user_id, user.id)
+ |> put_resp_cookie("logged_in", "true",
+ http_only: false,
+ max_age: 60 * 60 * 24 * 365 * 5000
+ )
+ end
+
def activate_form(conn, _params) do
user = conn.assigns[:current_user]
diff --git a/lib/plausible_web/templates/site/index.html.eex b/lib/plausible_web/templates/site/index.html.eex
index a8104197fd..0fab8fbf14 100644
--- a/lib/plausible_web/templates/site/index.html.eex
+++ b/lib/plausible_web/templates/site/index.html.eex
@@ -1,7 +1,7 @@
<div x-data="{selectedInvitation: null, invitationOpen: false, invitations: <%= Enum.map(@invitations, &({&1.invitation_id, &1})) |> Enum.into(%{}) |> Jason.encode! %>}" @keydown.escape.window="invitationOpen = false" class="container pt-6">
<%= if @needs_to_upgrade do %>
- <div class="rounded-md bg-yellow-50 p-4">
+ <div class="rounded-md bg-yellow-100 p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-yellow-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">