Support basePath in all queries.

This commit is contained in:
Mike Cao 2020-09-30 22:34:16 -07:00
parent 2508198a51
commit 5068ab12a9
11 changed files with 57 additions and 29 deletions

View File

@ -1,6 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { useRouter } from 'next/router';
import { post } from 'lib/web'; import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
@ -29,10 +30,11 @@ const validate = ({ user_id, username, password }) => {
}; };
export default function AccountEditForm({ values, onSave, onClose }) { export default function AccountEditForm({ values, onSave, onClose }) {
const { basePath } = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { ok, data } = await post(`/api/account`, values); const { ok, data } = await post(`${basePath}/api/account`, values);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -1,5 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { useRouter } from 'next/router';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { post } from 'lib/web'; import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
@ -37,10 +38,11 @@ const validate = ({ current_password, new_password, confirm_password }) => {
}; };
export default function ChangePasswordForm({ values, onSave, onClose }) { export default function ChangePasswordForm({ values, onSave, onClose }) {
const { basePath } = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { ok, data } = await post(`/api/account/password`, values); const { ok, data } = await post(`${basePath}/api/account/password`, values);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -1,4 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { useRouter } from 'next/router';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { del } from 'lib/web'; import { del } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
@ -8,7 +10,6 @@ import FormLayout, {
FormMessage, FormMessage,
FormRow, FormRow,
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import { FormattedMessage } from 'react-intl';
const CONFIRMATION_WORD = 'DELETE'; const CONFIRMATION_WORD = 'DELETE';
@ -27,15 +28,18 @@ const validate = ({ confirmation }) => {
}; };
export default function DeleteForm({ values, onSave, onClose }) { export default function DeleteForm({ values, onSave, onClose }) {
const { basePath } = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async ({ type, id }) => { const handleSubmit = async ({ type, id }) => {
const response = await del(`/api/${type}/${id}`); const { ok, data } = await del(`${basePath}/api/${type}/${id}`);
if (typeof response !== 'string') { if (ok) {
onSave(); onSave();
} else { } else {
setMessage(<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />); setMessage(
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
} }
}; };

View File

@ -1,7 +1,7 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import Router from 'next/router'; import { useRouter } from 'next/router';
import { post } from 'lib/web'; import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
@ -28,13 +28,17 @@ const validate = ({ username, password }) => {
}; };
export default function LoginForm() { export default function LoginForm() {
const router = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async ({ username, password }) => { const handleSubmit = async ({ username, password }) => {
const { ok, status, data } = await post('/api/auth/login', { username, password }); const { ok, status, data } = await post(`${router.basePath}/api/auth/login`, {
username,
password,
});
if (ok) { if (ok) {
await Router.push('/'); return router.push('/');
} else { } else {
setMessage( setMessage(
status === 401 ? ( status === 401 ? (

View File

@ -11,6 +11,7 @@ import FormLayout, {
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import Checkbox from 'components/common/Checkbox'; import Checkbox from 'components/common/Checkbox';
import { DOMAIN_REGEX } from 'lib/constants'; import { DOMAIN_REGEX } from 'lib/constants';
import { useRouter } from 'next/router';
const initialValues = { const initialValues = {
name: '', name: '',
@ -34,10 +35,11 @@ const validate = ({ name, domain }) => {
}; };
export default function WebsiteEditForm({ values, onSave, onClose }) { export default function WebsiteEditForm({ values, onSave, onClose }) {
const { basePath } = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { ok, data } = await post(`/api/website`, values); const { ok, data } = await post(`${basePath}/api/website`, values);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -1,4 +1,5 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { get } from 'lib/web'; import { get } from 'lib/web';
import enUS from 'public/country/en-US.json'; import enUS from 'public/country/en-US.json';
@ -8,9 +9,10 @@ const countryNames = {
export default function useCountryNames(locale) { export default function useCountryNames(locale) {
const [list, setList] = useState(countryNames[locale] || enUS); const [list, setList] = useState(countryNames[locale] || enUS);
const { basePath } = useRouter();
async function loadData(locale) { async function loadData(locale) {
const { ok, data } = await get(`/country/${locale}.json`); const { ok, data } = await get(`${basePath}/country/${locale}.json`);
if (ok) { if (ok) {
countryNames[locale] = data; countryNames[locale] = data;

View File

@ -2,6 +2,7 @@ import { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { get } from 'lib/web'; import { get } from 'lib/web';
import { updateQuery } from 'redux/actions/queries'; import { updateQuery } from 'redux/actions/queries';
import { useRouter } from 'next/router';
export default function useFetch(url, params = {}, options = {}) { export default function useFetch(url, params = {}, options = {}) {
const dispatch = useDispatch(); const dispatch = useDispatch();
@ -9,6 +10,7 @@ export default function useFetch(url, params = {}, options = {}) {
const [status, setStatus] = useState(); const [status, setStatus] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const [loading, setLoadiing] = useState(false); const [loading, setLoadiing] = useState(false);
const { basePath } = useRouter();
const keys = Object.keys(params) const keys = Object.keys(params)
.sort() .sort()
.map(key => params[key]); .map(key => params[key]);
@ -19,7 +21,7 @@ export default function useFetch(url, params = {}, options = {}) {
setLoadiing(true); setLoadiing(true);
setError(null); setError(null);
const time = performance.now(); const time = performance.now();
const { data, status } = await get(url, params); const { data, status } = await get(`${basePath}${url}`, params);
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() })); dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));

View File

@ -1,6 +1,8 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { updateUser } from 'redux/actions/user'; import { updateUser } from 'redux/actions/user';
import { useRouter } from 'next/router';
import { get } from '../lib/web';
export async function fetchUser() { export async function fetchUser() {
const res = await fetch('/api/auth/verify'); const res = await fetch('/api/auth/verify');
@ -13,29 +15,33 @@ export async function fetchUser() {
} }
export default function useRequireLogin() { export default function useRequireLogin() {
const router = useRouter();
const dispatch = useDispatch(); const dispatch = useDispatch();
const storeUser = useSelector(state => state.user); const storeUser = useSelector(state => state.user);
const [loading, setLoading] = useState(!storeUser); const [loading, setLoading] = useState(!storeUser);
const [user, setUser] = useState(storeUser); const [user, setUser] = useState(storeUser);
async function loadUser() {
setLoading(true);
const { ok, data } = await get(`${router.basePath}/api/auth/verify`);
if (!ok) {
return router.push('/login');
}
await dispatch(updateUser(data));
setUser(user);
setLoading(false);
}
useEffect(() => { useEffect(() => {
if (!loading && user) { if (!loading && user) {
return; return;
} }
setLoading(true); loadUser();
fetchUser().then(async user => {
if (!user) {
window.location.href = '/login';
return;
}
await dispatch(updateUser(user));
setUser(user);
setLoading(false);
});
}, []); }, []);
return { user, loading }; return { user, loading };

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "0.65.0", "version": "0.66.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ", "description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>", "author": "Mike Cao <mike@mikecao.com>",
"license": "MIT", "license": "MIT",

View File

@ -1,6 +1,6 @@
import { serialize } from 'cookie'; import { serialize } from 'cookie';
import { AUTH_COOKIE_NAME } from 'lib/constants'; import { AUTH_COOKIE_NAME } from 'lib/constants';
import { redirect } from 'lib/response'; import { ok } from 'lib/response';
export default async (req, res) => { export default async (req, res) => {
const cookie = serialize(AUTH_COOKIE_NAME, '', { const cookie = serialize(AUTH_COOKIE_NAME, '', {
@ -11,5 +11,5 @@ export default async (req, res) => {
res.setHeader('Set-Cookie', [cookie]); res.setHeader('Set-Cookie', [cookie]);
return redirect(res, '/login'); return ok(res);
}; };

View File

@ -1,8 +1,12 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useRouter } from 'next/router';
export default function LogoutPage() { export default function LogoutPage() {
const router = useRouter();
const { basePath } = router;
useEffect(() => { useEffect(() => {
fetch('/api/auth/logout').then(() => (window.location.href = '/login')); fetch(`${basePath}/api/auth/logout`).then(() => router.push('/login'));
}, []); }, []);
return null; return null;