Skip to content

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

Bookmarkable shared links (#752)

Uku Tahtcda031d4535b
diff --git a/assets/js/dashboard/api.js b/assets/js/dashboard/api.js
index d6fca56a2d..d8114f1f83 100644
--- a/assets/js/dashboard/api.js
+++ b/assets/js/dashboard/api.js
@@ -1,6 +1,7 @@
import {formatISO} from './date'
let abortController = new AbortController()
+let SHARED_LINK_AUTH = null
function serialize(obj) {
var str = [];
@@ -11,6 +12,10 @@ function serialize(obj) {
return str.join("&");
}
+export function setSharedLinkAuth(auth) {
+ SHARED_LINK_AUTH = auth
+}
+
export function cancelAll() {
abortController.abort()
abortController = new AbortController()
@@ -35,8 +40,9 @@ export function serializeQuery(query, extraQuery=[]) {
}
export function get(url, query, ...extraQuery) {
+ const headers = SHARED_LINK_AUTH ? {'X-Shared-Link-Auth': SHARED_LINK_AUTH} : {}
url = url + serializeQuery(query, extraQuery)
- return fetch(url, {signal: abortController.signal})
+ return fetch(url, {signal: abortController.signal, headers: headers})
.then( response => {
if (!response.ok) { throw response }
return response.json()
diff --git a/assets/js/dashboard/mount.js b/assets/js/dashboard/mount.js
index 2b93c539ef..a7b1e154b0 100644
--- a/assets/js/dashboard/mount.js
+++ b/assets/js/dashboard/mount.js
@@ -4,6 +4,7 @@ import 'url-search-params-polyfill';
import Router from './router'
import ErrorBoundary from './error-boundary'
+import * as api from './api'
const container = document.getElementById('stats-react-container')
@@ -16,6 +17,10 @@ if (container) {
}
const loggedIn = container.dataset.loggedIn === 'true'
+ const sharedLinkAuth = container.dataset.sharedLinkAuth
+ if (sharedLinkAuth) {
+ api.setSharedLinkAuth(sharedLinkAuth)
+ }
const app = (
<ErrorBoundary>
diff --git a/lib/plausible/auth/token.ex b/lib/plausible/auth/token.ex
index f53123f8c0..01b818e462 100644
--- a/lib/plausible/auth/token.ex
+++ b/lib/plausible/auth/token.ex
@@ -1,6 +1,16 @@
defmodule Plausible.Auth.Token do
@one_hour_in_seconds 30 * 60
+ def sign_shared_link(slug) do
+ Phoenix.Token.sign(PlausibleWeb.Endpoint, "shared-link", %{slug: slug})
+ end
+
+ def verify_shared_link(token) do
+ Phoenix.Token.verify(PlausibleWeb.Endpoint, "shared-link", token,
+ max_age: @one_hour_in_seconds * 24
+ )
+ end
+
def sign_password_reset(email) do
Phoenix.Token.sign(PlausibleWeb.Endpoint, "password-reset", %{email: email})
end
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 8b298ccdb6..1559dfd5e6 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -59,22 +59,39 @@ defmodule PlausibleWeb.StatsController do
|> send_resp(200, csv_content)
end
- def shared_link(conn, %{"slug" => slug}) do
+ def shared_link(conn, %{"slug" => domain, "auth" => auth}) do
shared_link =
- Repo.get_by(Plausible.Site.SharedLink, slug: slug)
+ Repo.get_by(Plausible.Site.SharedLink, slug: auth)
|> Repo.preload(:site)
- if shared_link do
+ if shared_link && shared_link.site.domain == domain do
if shared_link.password_hash do
- conn
- |> assign(:skip_plausible_tracking, true)
- |> render("shared_link_password.html",
- link: shared_link,
- layout: {PlausibleWeb.LayoutView, "focus.html"}
- )
+ with conn <- Plug.Conn.fetch_cookies(conn),
+ {:ok, token} <- Map.fetch(conn.req_cookies, "shared-link-token"),
+ {:ok, _} <- Plausible.Auth.Token.verify_shared_link(token) do
+ render_shared_link(conn, shared_link)
+ else
+ _e ->
+ conn
+ |> assign(:skip_plausible_tracking, true)
+ |> render("shared_link_password.html",
+ link: shared_link,
+ layout: {PlausibleWeb.LayoutView, "focus.html"}
+ )
+ end
else
- shared_link_auth_success(conn, shared_link)
+ render_shared_link(conn, shared_link)
end
+ end
+ end
+
+ def shared_link(conn, %{"slug" => slug}) do
+ shared_link =
+ Repo.get_by(Plausible.Site.SharedLink, slug: slug)
+ |> Repo.preload(:site)
+
+ if shared_link do
+ redirect(conn, to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")
else
render_error(conn, 404)
end
@@ -87,7 +104,11 @@ defmodule PlausibleWeb.StatsController do
if shared_link do
if Plausible.Auth.Password.match?(password, shared_link.password_hash) do
- shared_link_auth_success(conn, shared_link)
+ token = Plausible.Auth.Token.sign_shared_link(slug)
+
+ conn
+ |> put_resp_cookie("shared-link-token", token)
+ |> redirect(to: "/share/#{URI.encode_www_form(shared_link.site.domain)}?auth=#{slug}")
else
conn
|> assign(:skip_plausible_tracking, true)
@@ -102,14 +123,18 @@ defmodule PlausibleWeb.StatsController do
end
end
- defp shared_link_auth_success(conn, shared_link) do
- shared_link_key = "shared_link_auth_" <> shared_link.site.domain
-
+ defp render_shared_link(conn, shared_link) do
conn
- |> put_session(shared_link_key, %{
- valid_until: Timex.now() |> Timex.shift(hours: 1) |> DateTime.to_unix()
- })
- |> redirect(to: "/#{URI.encode_www_form(shared_link.site.domain)}")
+ |> assign(:skip_plausible_tracking, true)
+ |> put_resp_header("x-robots-tag", "noindex")
+ |> render("stats.html",
+ site: shared_link.site,
+ has_goals: Plausible.Sites.has_goals?(shared_link.site),
+ title: "Plausible · " <> shared_link.site.domain,
+ offer_email_report: false,
+ demo: false,
+ shared_link_auth: shared_link.slug
+ )
end
defp remove_email_report_banner(conn, site) do
diff --git a/lib/plausible_web/plugs/authorize_stats_plug.ex b/lib/plausible_web/plugs/authorize_stats_plug.ex
index 21f45dc8c2..4732fbe736 100644
--- a/lib/plausible_web/plugs/authorize_stats_plug.ex
+++ b/lib/plausible_web/plugs/authorize_stats_plug.ex
@@ -8,18 +8,20 @@ defmodule PlausibleWeb.AuthorizeStatsPlug do
def call(conn, _opts) do
site = Repo.get_by(Plausible.Site, domain: conn.params["domain"])
+ shared_link_auth = get_req_header(conn, "x-shared-link-auth") |> List.first()
+ # SHOULD GENERATE A NEW PHOENIX TOKEN WHEN MOUNTING THE STATS
+ shared_link_record =
+ shared_link_auth && Repo.get_by(Plausible.Site.SharedLink, slug: shared_link_auth)
if !site do
PlausibleWeb.ControllerHelpers.render_error(conn, 404) |> halt
else
user_id = get_session(conn, :current_user_id)
- shared_link_key = "shared_link_auth_" <> site.domain
- shared_link_auth = get_session(conn, shared_link_key)
can_access =
site.public ||
(user_id && Plausible.Sites.is_owner?(user_id, site)) ||
- (shared_link_auth && shared_link_auth[:valid_until] > DateTime.to_unix(Timex.now()))
+ (shared_link_auth && shared_link_record && shared_link_record.site_id == site.id)
if !can_access do
PlausibleWeb.ControllerHelpers.render_error(conn, 404) |> halt
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 93be81db35..73d2447ef7 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -15,6 +15,11 @@ defmodule PlausibleWeb.Router do
plug PlausibleWeb.LastSeenPlug
end
+ pipeline :shared_link do
+ plug :accepts, ["html"]
+ plug :put_secure_browser_headers
+ end
+
pipeline :csrf do
plug :protect_from_forgery
end
@@ -107,6 +112,13 @@ defmodule PlausibleWeb.Router do
post "/password/reset", AuthController, :password_reset
end
+ scope "/", PlausibleWeb do
+ pipe_through [:shared_link]
+
+ get "/share/:slug", StatsController, :shared_link
+ post "/share/:slug/authenticate", StatsController, :authenticate_shared_link
+ end
+
scope "/", PlausibleWeb do
pipe_through [:browser, :csrf]
@@ -197,8 +209,6 @@ defmodule PlausibleWeb.Router do
delete "/:website", SiteController, :delete_site
delete "/:website/stats", SiteController, :reset_stats
- get "/share/:slug", StatsController, :shared_link
- post "/share/:slug/authenticate", StatsController, :authenticate_shared_link
get "/:domain/visitors.csv", StatsController, :csv_export
get "/:domain/*path", StatsController, :stats
end
diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex
index f7ce51ff34..40568bb469 100644
--- a/lib/plausible_web/templates/layout/app.html.eex
+++ b/lib/plausible_web/templates/layout/app.html.eex
@@ -13,7 +13,7 @@
<script type="text/javascript" data-pref="<%= @conn.assigns[:current_user] && @conn.assigns[:current_user].theme %>" src="<%= Routes.static_path(@conn, "/js/applyTheme.js") %>"></script>
</head>
<body class="flex flex-col h-full bg-gray-50 dark:bg-gray-850">
- <nav class="relative py-8 z-10">
+ <nav class="relative z-10 py-8">
<div class="container">
<nav class="relative flex items-center justify-between sm:h-10 md:justify-center">
<div class="flex items-center flex-1 md:absolute md:inset-y-0 md:left-0">
@@ -24,35 +24,35 @@
</a>
</div>
</div>
- <div class="absolute flex items-center justify-end inset-y-0 right-0">
+ <div class="absolute inset-y-0 right-0 flex items-center justify-end">
<%= cond do %>
<% @conn.assigns[:current_user] -> %>
<ul class="flex">
<%= if Application.get_env(:plausible, :is_selfhost) do %>
- <li class="mr-6 hidden sm:block">
+ <li class="hidden mr-6 sm:block">
<%= link(to: "https://github.com/plausible/analytics", class: "font-bold rounded m-1 ml-0 p-1 hover:bg-gray-200 dark:hover:bg-gray-900 dark:text-gray-100", style: "line-height: 40px;", target: "_blank") do %>
- <svg class="w-4 h-4 inline -mt-1 mr-px" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub icon</title><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg>
+ <svg class="inline w-4 h-4 mr-px -mt-1" role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub icon</title><path fill="currentColor" d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg>
Repo
<% end %>
</li>
<% end %>
<%= if !Application.get_env(:plausible, :is_selfhost) && @conn.assigns[:current_user].subscription == nil do %>
- <li class="mr-6 hidden sm:block">
+ <li class="hidden mr-6 sm:block">
<%= link(trial_notificaton(@conn.assigns[:current_user]), to: "/settings", class: "font-bold text-orange-900 dark:text-yellow-900 rounded p-2 bg-orange-200 dark:bg-yellow-100", style: "line-height: 40px;") %>
</li>
<% else %>
- <li class="mr-6 hidden sm:block">
+ <li class="hidden mr-6 sm:block">
<%= link("Docs", to: "https://docs.plausible.io", class: "font-bold rounded m-1 p-1 hover:bg-gray-200 dark:hover:bg-gray-900 dark:text-gray-100", style: "line-height: 40px;", target: "_blank") %>
</li>
<% end %>
<li>
<div class="relative font-bold rounded">
- <div data-dropdown-trigger class="flex items-center hover:bg-gray-200 dark:hover:bg-gray-900 rounded m-1 p-1 cursor-pointer dark:text-gray-100">
+ <div data-dropdown-trigger class="flex items-center p-1 m-1 rounded cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-900 dark:text-gray-100">
<span class="pl-2 mr-2"><%= @conn.assigns[:current_user].name || @conn.assigns[:current_user].email %></span>
<svg style="height: 18px; transform: translateY(2px); fill: #606f7b;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 512 640" enable-background="new 0 0 512 512" xml:space="preserve"><g><circle cx="256" cy="52.8" r="50.8"/><circle cx="256" cy="256" r="50.8"/><circle cx="256" cy="459.2" r="50.8"/></g></svg>
</div>
- <div data-dropdown style="top: 42px; right: 0px; width: 185px;" class="dropdown-content hidden absolute right-0 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-500 rounded shadow-md z-10">
+ <div data-dropdown style="top: 42px; right: 0px; width: 185px;" class="absolute right-0 z-10 hidden bg-white border border-gray-300 rounded shadow-md dropdown-content dark:bg-gray-800 dark:border-gray-500">
<%= link("Settings", to: "/settings", class: "block py-2 px-2 border-b border-gray-300 dark:border-gray-500 hover:bg-gray-100 dark:hover:bg-gray-900 dark:text-gray-100") %>
<%= link("Log out", to: "/logout", method: :post, class: "block py-2 px-2 hover:bg-gray-100 dark:hover:bg-gray-900 dark:text-gray-100") %>
</div>
@@ -65,8 +65,8 @@
<% Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_authentication) -> %>
<ul class="flex" x-show="!document.cookie.includes('logged_in=true')">
<li>
- <div class="inline-flex rounded shadow ml-6">
- <a href="/" class="inline-flex items-center justify-center px-5 py-2 border border-transparent text-base leading-6 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:ring transition duration-150 ease-in-out">My Sites</a>
+ <div class="inline-flex ml-6 rounded shadow">
+ <a href="/" class="inline-flex items-center justify-center px-5 py-2 text-base font-medium text-white bg-indigo-600 border border-transparent leading-6 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring transition duration-150 ease-in-out">My Sites</a>
</div>
</li>
</ul>
@@ -84,8 +84,8 @@
<div class="inline-flex">
<a href="/login" class="font-medium text-gray-500 dark:text-gray-200 hover:text-gray-900 focus:outline-none focus:text-gray-900 transition duration-150 ease-in-out">Login</a>
</div>
- <div class="inline-flex rounded shadow ml-6">
- <a href="/register" class="inline-flex items-center justify-center px-5 py-2 border border-transparent text-base leading-6 font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:ring transition duration-150 ease-in-out">Sign up</a>
+ <div class="inline-flex ml-6 rounded shadow">
+ <a href="/register" class="inline-flex items-center justify-center px-5 py-2 text-base font-medium text-white bg-indigo-600 border border-transparent leading-6 rounded-md hover:bg-indigo-500 focus:outline-none focus:ring transition duration-150 ease-in-out">Sign up</a>
</div>
</li>
</ul>
@@ -95,21 +95,23 @@
</div>
</nav>
- <%= render("_flash.html", assigns) %>
+ <%= if @conn.private[:phoenix_flash] do %>
+ <%= render("_flash.html", assigns) %>
+ <% end %>
<%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "past_due" do %>
<div class="container">
- <div class="p-2 rounded-lg bg-yellow-100 sm:p-3">
- <div class="flex items-center justify-between flex-wrap">
- <div class="w-0 flex-1 flex items-center">
- <svg class="h-6 w-6 text-yellow-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <div class="p-2 bg-yellow-100 rounded-lg sm:p-3">
+ <div class="flex flex-wrap items-center justify-between">
+ <div class="flex items-center flex-1 w-0">
+ <svg class="w-6 h-6 text-yellow-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 9V11M12 15H12.01M5.07183 19H18.9282C20.4678 19 21.4301 17.3333 20.6603 16L13.7321 4C12.9623 2.66667 11.0378 2.66667 10.268 4L3.33978 16C2.56998 17.3333 3.53223 19 5.07183 19Z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<p class="ml-3 font-medium text-yellow-800">
Your latest payment failed. Please provide valid payment details to keep using Plausible.
</p>
</div>
- <div class="order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto">
+ <div class="flex-shrink-0 order-3 w-full mt-2 sm:order-2 sm:mt-0 sm:w-auto">
<div class="rounded-md shadow-sm">
<%= link("Update billing info", to: @conn.assigns[:current_user].subscription.update_url, class: "flex items-center justify-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-gray-600 dark:text-gray-400 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-200 focus:outline-none focus:ring transition ease-in-out duration-150") %>
</div>
@@ -121,17 +123,17 @@
<%= if @conn.assigns[:current_user] && @conn.assigns[:current_user].subscription && @conn.assigns[:current_user].subscription.status == "paused" do %>
<div class="container">
- <div class="p-2 rounded-lg bg-red-100 sm:p-3">
- <div class="flex items-center justify-between flex-wrap">
- <div class="w-0 flex-1 flex items-center">
- <svg class="h-6 w-6 text-red-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <div class="p-2 bg-red-100 rounded-lg sm:p-3">
+ <div class="flex flex-wrap items-center justify-between">
+ <div class="flex items-center flex-1 w-0">
+ <svg class="w-6 h-6 text-red-800" viewBox="0 0 24 24" stroke="currentColor" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 9V11M12 15H12.01M5.07183 19H18.9282C20.4678 19 21.4301 17.3333 20.6603 16L13.7321 4C12.9623 2.66667 11.0378 2.66667 10.268 4L3.33978 16C2.56998 17.3333 3.53223 19 5.07183 19Z" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<p class="ml-3 font-medium text-red-800">
Your subscription is paused due to failed payments. Please provide valid payment details to keep using Plausible.
</p>
</div>
- <div class="order-3 mt-2 flex-shrink-0 w-full sm:order-2 sm:mt-0 sm:w-auto">
+ <div class="flex-shrink-0 order-3 w-full mt-2 sm:order-2 sm:mt-0 sm:w-auto">
<div class="rounded-md shadow-sm">
<%= link("Update billing info", to: @conn.assigns[:current_user].subscription.update_url, class: "flex items-center justify-center px-4 py-2 border border-transparent text-sm leading-5 font-medium rounded-md text-gray-600 dark:text-gray-400 bg-white dark:bg-gray-800 hover:text-gray-500 dark:hover:text-gray-200 focus:outline-none focus:ring transition ease-in-out duration-150") %>
</div>
diff --git a/lib/plausible_web/templates/layout/focus.html.eex b/lib/plausible_web/templates/layout/focus.html.eex
index e7a6abde3b..95f7c6be2a 100644
--- a/lib/plausible_web/templates/layout/focus.html.eex
+++ b/lib/plausible_web/templates/layout/focus.html.eex
@@ -12,16 +12,19 @@
<script type="text/javascript" data-pref="<%= @conn.assigns[:current_user] && @conn.assigns[:current_user].theme %>" src="<%= Routes.static_path(@conn, "/js/applyTheme.js") %>"></script>
</head>
<body class="flex flex-col h-full bg-gray-100 dark:bg-gray-900">
- <div class="text-center w-full my-8">
+ <div class="w-full my-8 text-center">
<a href="<%= home_dest(@conn) %>">
<%= img_tag(PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo_dark.png"), class: "hidden dark:inline", style: "height: 2.5rem;", alt: "Plausible logo") %>
<%= img_tag(PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo.png"), class: "inline dark:hidden", style: "height: 2.5rem;", alt: "Plausible logo")%>
</a>
</div>
- <%= render("_flash.html", assigns) %>
+ <%= if @conn.private[:phoenix_flash] do %>
+ <%= render("_flash.html", assigns) %>
+ <% end %>
<%= @inner_content %>
+
<p class="text-center text-gray-500 text-xs py-8">
©<%= DateTime.utc_now().year() %> Plausible Analytics. All rights reserved.
</p>
diff --git a/lib/plausible_web/templates/site/settings_visibility.html.eex b/lib/plausible_web/templates/site/settings_visibility.html.eex
index 30e56700de..bf8c45d61a 100644
--- a/lib/plausible_web/templates/site/settings_visibility.html.eex
+++ b/lib/plausible_web/templates/site/settings_visibility.html.eex
@@ -36,7 +36,7 @@
<div class="mt-6">
<%= for link <- @shared_links do %>
<div class="flex relative w-full max-w-xl mt-2 text-sm">
- <input type="text" id="<%= link.slug %>" readonly="readonly" value="<%= shared_link_dest(link) %>" class="transition bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded rounded-r-none w-full p-2 text-gray-700 dark:text-gray-300 focus:outline-none focus:border-gray-300 dark:focus:border-gray-500" />
+ <input type="text" id="<%= link.slug %>" readonly="readonly" value="<%= shared_link_dest(@site, link) %>" class="transition bg-gray-100 dark:bg-gray-900 outline-none appearance-none border border-transparent rounded rounded-r-none w-full p-2 text-gray-700 dark:text-gray-300 focus:outline-none focus:border-gray-300 dark:focus:border-gray-500" />
<button onclick="var input = document.getElementById('<%= link.slug %>'); input.focus(); input.select(); document.execCommand('copy');" href="javascript:void(0)" class="py-2 px-4 bg-gray-200 dark:bg-gray-850 text-indigo-800 dark:text-indigo-500 rounded-none border-r border-gray-300 dark:border-gray-500 hover:bg-gray-300 dark:hover:bg-gray-825">
<svg class="feather-sm" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>
</button>
diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex
index 3e61d82daa..0acb0ea72d 100644
--- a/lib/plausible_web/templates/stats/stats.html.eex
+++ b/lib/plausible_web/templates/stats/stats.html.eex
@@ -5,7 +5,7 @@
</div>
<% end %>
<div class="pt-6"></div>
- <div id="stats-react-container" data-domain="<%= @site.domain %>" data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= [email protected][:current_user] %>" data-inserted-at="<%= @site.inserted_at %>"></div>
+ <div id="stats-react-container" data-domain="<%= @site.domain %>" data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= [email protected][:current_user] %>" data-inserted-at="<%= @site.inserted_at %>" data-shared-link-auth="<%= assigns[:shared_link_auth] %>"></div>
<div id="modal_root"></div>
<%= if [email protected][:current_user] && @conn.assigns[:demo] do %>
<div class="bg-gray-50 dark:bg-gray-850">
diff --git a/lib/plausible_web/views/site_view.ex b/lib/plausible_web/views/site_view.ex
index 7a1014e59e..f93235d5c2 100644
--- a/lib/plausible_web/views/site_view.ex
+++ b/lib/plausible_web/views/site_view.ex
@@ -21,8 +21,9 @@ defmodule PlausibleWeb.SiteView do
name
end
- def shared_link_dest(link) do
- plausible_url() <> "/share/" <> link.slug
+ def shared_link_dest(site, link) do
+ domain = "/share/#{URI.encode_www_form(site.domain)}"
+ plausible_url() <> domain <> "?auth=" <> link.slug
end
def snippet(site) do
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index b3b4e4bff2..a125b2cfe3 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -57,7 +57,7 @@ defmodule PlausibleWeb.StatsControllerTest do
link =
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
- conn = get(conn, "/share/#{link.slug}")
+ conn = get(conn, "/share/#{site.domain}?auth=#{link.slug}")
assert response(conn, 200) =~ "Enter password"
end
@@ -67,20 +67,9 @@ defmodule PlausibleWeb.StatsControllerTest do
site = insert(:site, domain: "test-site.com")
link = insert(:shared_link, site: site)
- conn = get(conn, "/share/#{link.slug}")
- assert redirected_to(conn, 302) == "/#{site.domain}"
-
- conn = get(conn, "/#{site.domain}")
+ conn = get(conn, "/share/test-site.com/?auth=#{link.slug}")
assert html_response(conn, 200) =~ "stats-react-container"
end
-
- test "encodes URI when redirecting", %{conn: conn} do
- site = insert(:site, domain: "test-site.com/wat")
- link = insert(:shared_link, site: site)
-
- conn = get(conn, "/share/#{link.slug}")
- assert redirected_to(conn, 302) == "/test-site.com%2Fwat"
- end
end
describe "POST /share/:slug/authenticate" do
@@ -91,9 +80,9 @@ defmodule PlausibleWeb.StatsControllerTest do
insert(:shared_link, site: site, password_hash: Plausible.Auth.Password.hash("password"))
conn = post(conn, "/share/#{link.slug}/authenticate", %{password: "password"})
- assert redirected_to(conn, 302) == "/#{site.domain}"
+ assert redirected_to(conn, 302) == "/share/#{site.domain}?auth=#{link.slug}"
- conn = get(conn, "/#{site.domain}")
+ conn = get(conn, "/share/#{site.domain}?auth=#{link.slug}")
assert html_response(conn, 200) =~ "stats-react-container"
end