Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Merge branch 'dev' of https://github.com/umami-software/umami into dev

Mike Caod784b2a8db5b
diff --cc components/common/RefreshButton.js
index 0b197bd2e,0b197bd2e..be1a8a1d8
--- a/components/common/RefreshButton.js
+++ b/components/common/RefreshButton.js
@@@ -11,7 -11,7 +11,7 @@@ import useDateRange from 'hooks/useDate
function RefreshButton({ websiteId }) {
const [dateRange] = useDateRange(websiteId);
const [loading, setLoading] = useState(false);
-- const selector = useCallback(state => state[`/website/${websiteId}/stats`], [websiteId]);
++ const selector = useCallback(state => state[`/websites/${websiteId}/stats`], [websiteId]);
const completed = useStore(selector);
function handleClick() {
diff --cc components/forms/AccountEditForm.js
index 868256298,97317aff4..bedc3b82d
--- a/components/forms/AccountEditForm.js
+++ b/components/forms/AccountEditForm.js
@@@ -33,8 -33,7 +33,8 @@@ export default function AccountEditForm
const [message, setMessage] = useState();
const handleSubmit = async values => {
- const { ok, data } = await post('/account', values);
+ const { user_id } = values;
- const { ok, data } = await post(user_id ? `/account/${user_id}` : '/account', values);
++ const { ok, data } = await post(user_id ? `/accounts/${user_id}` : '/accounts', values);
if (ok) {
onSave();
diff --cc components/forms/ChangePasswordForm.js
index 29f345216,29f345216..4870d523e
--- a/components/forms/ChangePasswordForm.js
+++ b/components/forms/ChangePasswordForm.js
@@@ -9,6 -9,6 +9,7 @@@ import FormLayout,
FormRow,
} from 'components/layout/FormLayout';
import useApi from 'hooks/useApi';
++import useUser from '../../hooks/useUser';
const initialValues = {
current_password: '',
@@@ -39,9 -39,9 +40,10 @@@ const validate = ({ current_password, n
export default function ChangePasswordForm({ values, onSave, onClose }) {
const { post } = useApi();
const [message, setMessage] = useState();
++ const { user } = useUser();
const handleSubmit = async values => {
-- const { ok, data } = await post('/account/password', values);
++ const { ok, data } = await post(`/accounts/${user.user_id}/password`, values);
if (ok) {
onSave();
diff --cc components/forms/WebsiteEditForm.js
index 80a6dfc0c,80a6dfc0c..21daeeaf8
--- a/components/forms/WebsiteEditForm.js
+++ b/components/forms/WebsiteEditForm.js
@@@ -38,7 -38,7 +38,6 @@@ const validate = ({ name, domain }) =>
};
const OwnerDropDown = ({ user, accounts }) => {
-- console.info(styles);
const { setFieldValue, values } = useFormikContext();
useEffect(() => {
@@@ -79,7 -79,7 +78,8 @@@ export default function WebsiteEditForm
const [message, setMessage] = useState();
const handleSubmit = async values => {
-- const { ok, data } = await post('/website', values);
++ const { website_id } = values;
++ const { ok, data } = await post(website_id ? `/websites/${website_id}` : '/websites', values);
if (ok) {
onSave();
@@@ -137,6 -137,6 +137,7 @@@
defaultMessage="Enable share URL"
/>
}
++ value={null}
/>
)}
</Field>
diff --cc components/metrics/ActiveUsers.js
index 653fc783a,653fc783a..0ab266d3f
--- a/components/metrics/ActiveUsers.js
+++ b/components/metrics/ActiveUsers.js
@@@ -6,7 -6,7 +6,7 @@@ import Dot from 'components/common/Dot'
import styles from './ActiveUsers.module.css';
export default function ActiveUsers({ websiteId, className, value, interval = 60000 }) {
-- const url = websiteId ? `/website/${websiteId}/active` : null;
++ const url = websiteId ? `/websites/${websiteId}/active` : null;
const { data } = useFetch(url, {
interval,
});
diff --cc components/metrics/EventsChart.js
index 50dc940d2,50dc940d2..a8735b5d6
--- a/components/metrics/EventsChart.js
+++ b/components/metrics/EventsChart.js
@@@ -16,7 -16,7 +16,7 @@@ export default function EventsChart({ w
} = usePageQuery();
const { data, loading } = useFetch(
-- `/website/${websiteId}/events`,
++ `/websites/${websiteId}/events`,
{
params: {
start_at: +startDate,
diff --cc components/metrics/MetricsBar.js
index 21928b617,21928b617..aa260c8bc
--- a/components/metrics/MetricsBar.js
+++ b/components/metrics/MetricsBar.js
@@@ -19,7 -19,7 +19,7 @@@ export default function MetricsBar({ we
} = usePageQuery();
const { data, error, loading } = useFetch(
-- `/website/${websiteId}/stats`,
++ `/websites/${websiteId}/stats`,
{
params: {
start_at: +startDate,
diff --cc components/metrics/MetricsTable.js
index fc62e518c,fc62e518c..9079d1902
--- a/components/metrics/MetricsTable.js
+++ b/components/metrics/MetricsTable.js
@@@ -38,7 -38,7 +38,7 @@@ export default function MetricsTable(
const { formatMessage } = useIntl();
const { data, loading, error } = useFetch(
-- `/website/${websiteId}/metrics`,
++ `/websites/${websiteId}/metrics`,
{
params: {
type,
diff --cc components/metrics/WebsiteChart.js
index fd74538f1,fd74538f1..48b97f269
--- a/components/metrics/WebsiteChart.js
+++ b/components/metrics/WebsiteChart.js
@@@ -35,7 -35,7 +35,7 @@@ export default function WebsiteChart(
const { get } = useApi();
const { data, loading, error } = useFetch(
-- `/website/${websiteId}/pageviews`,
++ `/websites/${websiteId}/pageviews`,
{
params: {
start_at: +startDate,
@@@ -70,7 -70,7 +70,7 @@@
async function handleDateChange(value) {
if (value === 'all') {
-- const { data, ok } = await get(`/website/${websiteId}`);
++ const { data, ok } = await get(`/websites/${websiteId}`);
if (ok) {
setDateRange({ value, ...getDateRangeValues(new Date(data.created_at), Date.now()) });
}
diff --cc components/metrics/WebsiteHeader.js
index 15f6d0ff9,15f6d0ff9..1a6bdf159
--- a/components/metrics/WebsiteHeader.js
+++ b/components/metrics/WebsiteHeader.js
@@@ -17,8 -17,8 +17,8 @@@ export default function WebsiteHeader(
<Favicon domain={domain} />
<Link
className={styles.titleLink}
-- href="/website/[...id]"
-- as={`/website/${websiteId}/${title}`}
++ href="/websites/[...id]"
++ as={`/websites/${websiteId}/${title}`}
>
<OverflowText tooltipId={`${websiteId}-title`}>{title}</OverflowText>
</Link>
@@@ -41,8 -41,8 +41,8 @@@
<RefreshButton websiteId={websiteId} />
{showLink && (
<Link
-- href="/website/[...id]"
-- as={`/website/${websiteId}/${title}`}
++ href="/websites/[...id]"
++ as={`/websites/${websiteId}/${title}`}
className={styles.link}
icon={<Arrow />}
size="small"
diff --cc components/pages/WebsiteDetails.js
index 78c5f7520,78c5f7520..3fc234a47
--- a/components/pages/WebsiteDetails.js
+++ b/components/pages/WebsiteDetails.js
@@@ -52,7 -52,7 +52,7 @@@ const views =
};
export default function WebsiteDetails({ websiteId }) {
-- const { data } = useFetch(`/website/${websiteId}`);
++ const { data } = useFetch(`/websites/${websiteId}`);
const [chartLoaded, setChartLoaded] = useState(false);
const [countryData, setCountryData] = useState();
const [eventsData, setEventsData] = useState();
diff --cc components/settings/WebsiteSettings.js
index 451be47f7,451be47f7..49bd005fb
--- a/components/settings/WebsiteSettings.js
+++ b/components/settings/WebsiteSettings.js
@@@ -86,8 -86,8 +86,8 @@@ export default function WebsiteSettings
const DetailsLink = ({ website_id, name, domain }) => (
<Link
className={styles.detailLink}
-- href="/website/[...id]"
-- as={`/website/${website_id}/${name}`}
++ href="/websites/[...id]"
++ as={`/websites/${website_id}/${name}`}
>
<Favicon domain={domain} />
<OverflowText tooltipId={`${website_id}-name`}>{name}</OverflowText>
diff --cc pages/api/account/index.js
index 236538bcd,a3fc6bc17..000000000
deleted file mode 100644,100644
--- a/pages/api/account/index.js
+++ /dev/null
@@@ -1,27 -1,62 +1,0 @@@
--import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
- import { getAccountByUsername, createAccount } from 'queries';
-import { getAccountById, getAccountByUsername, updateAccount, createAccount } from 'queries';
--import { useAuth } from 'lib/middleware';
-import { uuid } from 'lib/crypto';
--
--export default async (req, res) => {
- await useAuth(req, res);
-
- const { user_id: current_user_id, is_admin: current_user_is_admin } = req.auth;
-
-- if (req.method === 'POST') {
- await useAuth(req, res);
- const { user_id, username, password, is_admin } = req.body;
--
- if (!req.auth.is_admin) {
- return unauthorized(res);
- }
- if (user_id) {
- const account = await getAccountById(user_id);
--
- const { username, password } = req.body;
- if (account.user_id === current_user_id || current_user_is_admin) {
- const data = {};
--
- const accountByUsername = await getAccountByUsername(username);
- if (password) {
- data.password = hashPassword(password);
- }
--
- if (accountByUsername) {
- return badRequest(res, 'Account already exists');
- }
- // Only admin can change these fields
- if (current_user_is_admin) {
- data.username = username;
- data.is_admin = is_admin;
- }
--
- const created = await createAccount({ username, password: hashPassword(password) });
- if (data.username && account.username !== data.username) {
- const accountByUsername = await getAccountByUsername(username);
--
- return ok(res, created);
- if (accountByUsername) {
- return badRequest(res, 'Account already exists');
- }
- }
-
- const updated = await updateAccount(user_id, data);
-
- return ok(res, updated);
- }
-
- return unauthorized(res);
- } else {
- const accountByUsername = await getAccountByUsername(username);
-
- if (accountByUsername) {
- return badRequest(res, 'Account already exists');
- }
-
- const created = await createAccount({
- username,
- password: hashPassword(password),
- account_uuid: uuid(),
- });
-
- return ok(res, created);
- }
-- }
--
-- return methodNotAllowed(res);
--};
diff --cc pages/api/accounts/[id]/index.js
index e5020f1fd,000000000..e5020f1fd
mode 100644,000000..100644
--- a/pages/api/accounts/[id]/index.js
+++ b/pages/api/accounts/[id]/index.js
diff --cc pages/api/accounts/[id]/password.js
index da3a643c1,da3a643c1..968582059
--- a/pages/api/accounts/[id]/password.js
+++ b/pages/api/accounts/[id]/password.js
@@@ -12,24 -12,24 +12,25 @@@ import
export default async (req, res) => {
await useAuth(req, res);
-- const { user_id: auth_user_id, is_admin } = req.auth;
-- const { user_id, current_password, new_password } = req.body;
++ const { user_id: currentUserId, is_admin: currentUserIsAdmin } = req.auth;
++ const { current_password, new_password } = req.body;
++ const { id } = req.query;
++ const userId = +id;
-- if (!is_admin && user_id !== auth_user_id) {
++ if (!currentUserIsAdmin && userId !== currentUserId) {
return unauthorized(res);
}
if (req.method === 'POST') {
-- const account = await getAccountById(user_id);
-- const valid = checkPassword(current_password, account.password);
++ const account = await getAccountById(userId);
-- if (!valid) {
++ if (!checkPassword(current_password, account.password)) {
return badRequest(res, 'Current password is incorrect');
}
const password = hashPassword(new_password);
-- const updated = await updateAccount(user_id, { password });
++ const updated = await updateAccount(userId, { password });
return ok(res, updated);
}
diff --cc pages/api/accounts/index.js
index 42b96a11d,42b96a11d..acaf1451b
--- a/pages/api/accounts/index.js
+++ b/pages/api/accounts/index.js
@@@ -1,6 -1,6 +1,7 @@@
--import { getAccounts } from 'queries';
++import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
import { useAuth } from 'lib/middleware';
--import { ok, unauthorized, methodNotAllowed } from 'next-basics';
++import { uuid } from 'lib/crypto';
++import { createAccount, getAccountByUsername, getAccounts } from 'queries';
export default async (req, res) => {
await useAuth(req, res);
@@@ -17,5 -17,5 +18,29 @@@
return ok(res, accounts);
}
++ if (req.method === 'POST') {
++ await useAuth(req, res);
++
++ if (!req.auth.is_admin) {
++ return unauthorized(res);
++ }
++
++ const { username, password } = req.body;
++
++ const accountByUsername = await getAccountByUsername(username);
++
++ if (accountByUsername) {
++ return badRequest(res, 'Account already exists');
++ }
++
++ const created = await createAccount({
++ username,
++ password: hashPassword(password),
++ account_uuid: uuid(),
++ });
++
++ return ok(res, created);
++ }
++
return methodNotAllowed(res);
};
diff --cc pages/api/website/[id]/index.js
index aaebb3d41,aaebb3d41..000000000
deleted file mode 100644,100644
--- a/pages/api/website/[id]/index.js
+++ /dev/null
@@@ -1,34 -1,34 +1,0 @@@
--import { methodNotAllowed, ok, unauthorized } from 'next-basics';
--import { deleteWebsite, getWebsiteById } from 'queries';
--import { allowQuery } from 'lib/auth';
--import { useCors } from 'lib/middleware';
--
--export default async (req, res) => {
-- const { id } = req.query;
--
-- const websiteId = +id;
--
-- if (req.method === 'GET') {
-- await useCors(req, res);
--
-- if (!(await allowQuery(req))) {
-- return unauthorized(res);
-- }
--
-- const website = await getWebsiteById(websiteId);
--
-- return ok(res, website);
-- }
--
-- if (req.method === 'DELETE') {
-- if (!(await allowQuery(req, true))) {
-- return unauthorized(res);
-- }
--
-- await deleteWebsite(websiteId);
--
-- return ok(res);
-- }
--
-- return methodNotAllowed(res);
--};
diff --cc pages/api/website/index.js
index ac02de856,ac02de856..000000000
deleted file mode 100644,100644
--- a/pages/api/website/index.js
+++ /dev/null
@@@ -1,44 -1,44 +1,0 @@@
--import { ok, unauthorized, methodNotAllowed, getRandomChars } from 'next-basics';
--import { updateWebsite, createWebsite, getWebsiteById } from 'queries';
--import { useAuth } from 'lib/middleware';
--import { uuid } from 'lib/crypto';
--
--export default async (req, res) => {
-- await useAuth(req, res);
--
-- const { user_id, is_admin } = req.auth;
-- const { website_id, enable_share_url } = req.body;
--
-- if (req.method === 'POST') {
-- const { name, domain, owner } = req.body;
-- const website_owner = parseInt(owner);
--
-- if (website_id) {
-- const website = await getWebsiteById(website_id);
--
-- if (website.user_id !== user_id && !is_admin) {
-- return unauthorized(res);
-- }
--
-- let { share_id } = website;
--
-- if (enable_share_url) {
-- share_id = share_id ? share_id : getRandomChars(8);
-- } else {
-- share_id = null;
-- }
--
-- await updateWebsite(website_id, { name, domain, share_id, user_id: website_owner });
--
-- return ok(res);
-- } else {
-- const website_uuid = uuid();
-- const share_id = enable_share_url ? getRandomChars(8) : null;
-- const website = await createWebsite(website_owner, { website_uuid, name, domain, share_id });
--
-- return ok(res, website);
-- }
-- }
--
-- return methodNotAllowed(res);
--};
diff --cc pages/api/websites/[id]/index.js
index 000000000,000000000..bc9cb17fd
new file mode 100644
--- /dev/null
+++ b/pages/api/websites/[id]/index.js
@@@ -1,0 -1,0 +1,59 @@@
++import { getRandomChars, methodNotAllowed, ok, unauthorized } from 'next-basics';
++import { deleteWebsite, getWebsiteById, updateWebsite } from 'queries';
++import { allowQuery } from 'lib/auth';
++import { useAuth, useCors } from 'lib/middleware';
++
++export default async (req, res) => {
++ const { id } = req.query;
++
++ const websiteId = +id;
++
++ if (req.method === 'GET') {
++ await useCors(req, res);
++
++ if (!(await allowQuery(req))) {
++ return unauthorized(res);
++ }
++
++ const website = await getWebsiteById(websiteId);
++
++ return ok(res, website);
++ }
++
++ if (req.method === 'POST') {
++ await useAuth(req, res);
++
++ const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;
++ const { name, domain, owner, enable_share_url } = req.body;
++
++ const website = await getWebsiteById(websiteId);
++
++ if (website.user_id !== currentUserId && !currentUserIsAdmin) {
++ return unauthorized(res);
++ }
++
++ let { share_id } = website;
++
++ if (enable_share_url) {
++ share_id = share_id ? share_id : getRandomChars(8);
++ } else {
++ share_id = null;
++ }
++
++ await updateWebsite(websiteId, { name, domain, share_id, user_id: +owner });
++
++ return ok(res);
++ }
++
++ if (req.method === 'DELETE') {
++ if (!(await allowQuery(req, true))) {
++ return unauthorized(res);
++ }
++
++ await deleteWebsite(websiteId);
++
++ return ok(res);
++ }
++
++ return methodNotAllowed(res);
++};
diff --cc pages/api/websites/index.js
index 8b03a9e98,8b03a9e98..108a4a8a4
--- a/pages/api/websites/index.js
+++ b/pages/api/websites/index.js
@@@ -1,6 -1,6 +1,7 @@@
--import { getAllWebsites, getUserWebsites } from 'queries';
++import { createWebsite, getAllWebsites, getUserWebsites } from 'queries';
++import { ok, methodNotAllowed, unauthorized, getRandomChars } from 'next-basics';
import { useAuth } from 'lib/middleware';
--import { ok, methodNotAllowed, unauthorized } from 'next-basics';
++import { uuid } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
@@@ -22,5 -22,5 +23,24 @@@
return ok(res, websites);
}
++ if (req.method === 'POST') {
++ await useAuth(req, res);
++
++ const { is_admin: currentUserIsAdmin, user_id: currentUserId } = req.auth;
++ const { name, domain, owner, enable_share_url } = req.body;
++
++ const website_owner = +owner;
++
++ if (website_owner !== currentUserId && !currentUserIsAdmin) {
++ return unauthorized(res);
++ }
++
++ const website_uuid = uuid();
++ const share_id = enable_share_url ? getRandomChars(8) : null;
++ const website = await createWebsite(website_owner, { website_uuid, name, domain, share_id });
++
++ return ok(res, website);
++ }
++
return methodNotAllowed(res);
};