mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 09:45:04 +01:00
Move auth token logic to useApi hook.
This commit is contained in:
parent
d552a01701
commit
be98500e82
@ -36,7 +36,7 @@ export default function WebsiteSettings() {
|
||||
const [showUrl, setShowUrl] = useState();
|
||||
const [saved, setSaved] = useState(0);
|
||||
const [message, setMessage] = useState();
|
||||
const { data } = useFetch(`/websites` + (user?.is_admin ? '?include_all=true' : ''), {}, [saved]);
|
||||
const { data } = useFetch('/websites', { params: { include_all: !!user?.is_admin } }, [saved]);
|
||||
|
||||
const Buttons = row => (
|
||||
<ButtonLayout align="right">
|
||||
|
@ -3,7 +3,7 @@ import { useRouter } from 'next/router';
|
||||
import { get, post, put, del, getItem } from 'lib/web';
|
||||
import { AUTH_TOKEN } from 'lib/constants';
|
||||
|
||||
function includeAuthToken(headers) {
|
||||
function includeAuthToken(headers = {}) {
|
||||
const authToken = getItem(AUTH_TOKEN);
|
||||
|
||||
if (authToken) {
|
||||
@ -18,29 +18,29 @@ export default function useApi() {
|
||||
|
||||
return {
|
||||
get: useCallback(
|
||||
async (url, params, headers = {}) => {
|
||||
return get(`${basePath}/api/${url}`, params, includeAuthToken(headers));
|
||||
async (url, params, headers) => {
|
||||
return get(`${basePath}/api${url}`, params, includeAuthToken(headers));
|
||||
},
|
||||
[get],
|
||||
),
|
||||
|
||||
post: useCallback(
|
||||
async (url, params, headers) => {
|
||||
return post(`${basePath}/api/${url}`, params, includeAuthToken(headers));
|
||||
return post(`${basePath}/api${url}`, params, includeAuthToken(headers));
|
||||
},
|
||||
[post],
|
||||
),
|
||||
|
||||
put: useCallback(
|
||||
async (url, params, headers) => {
|
||||
return put(`${basePath}/api/${url}`, params, includeAuthToken(headers));
|
||||
return put(`${basePath}/api${url}`, params, includeAuthToken(headers));
|
||||
},
|
||||
[put],
|
||||
),
|
||||
|
||||
del: useCallback(
|
||||
async (url, params, headers) => {
|
||||
return del(`${basePath}/api/${url}`, params, includeAuthToken(headers));
|
||||
return del(`${basePath}/api${url}`, params, includeAuthToken(headers));
|
||||
},
|
||||
[del],
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user