diff --git a/components/forms/LoginForm.js b/components/forms/LoginForm.js index f73241ac..3c131558 100644 --- a/components/forms/LoginForm.js +++ b/components/forms/LoginForm.js @@ -46,7 +46,9 @@ export default function LoginForm() { setUser(data.user); - return router.push('/'); + await router.push('/'); + + return null; } else { setMessage( status === 401 ? ( diff --git a/components/layout/Footer.module.css b/components/layout/Footer.module.css index a83c8c3c..c04fd9fc 100644 --- a/components/layout/Footer.module.css +++ b/components/layout/Footer.module.css @@ -9,10 +9,11 @@ .version { text-align: right; + padding-right: 10px; } @media only screen and (max-width: 768px) { - .version { + .footer .version { text-align: center; } } diff --git a/components/pages/Settings.js b/components/pages/Settings.js index d38be07d..217ad590 100644 --- a/components/pages/Settings.js +++ b/components/pages/Settings.js @@ -18,6 +18,10 @@ export default function Settings() { const router = useRouter(); const { pathname } = router; + if (!user) { + return null; + } + const menuOptions = [ { label: , diff --git a/hooks/useApi.js b/hooks/useApi.js index 57dd9bd6..fd17cde0 100644 --- a/hooks/useApi.js +++ b/hooks/useApi.js @@ -1,25 +1,48 @@ import { useCallback } from 'react'; import { useRouter } from 'next/router'; -import { get, post, put, del } from 'lib/web'; +import { get, post, put, del, getItem } from 'lib/web'; +import { AUTH_TOKEN } from 'lib/constants'; + +function includeAuthToken(headers) { + const authToken = getItem(AUTH_TOKEN); + + if (authToken) { + headers.Authorization = `Bearer ${authToken}`; + } + + return headers; +} export default function useApi() { const { basePath } = useRouter(); return { - get: useCallback(async (url, params, headers) => { - return get(`${basePath}/api/${url}`, params, headers); - }, []), + get: useCallback( + 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, headers); - }, []), + post: useCallback( + async (url, params, headers) => { + return post(`${basePath}/api/${url}`, params, includeAuthToken(headers)); + }, + [post], + ), - put: useCallback(async (url, params, headers) => { - return put(`${basePath}/api/${url}`, params, headers); - }, []), + put: useCallback( + async (url, params, headers) => { + return put(`${basePath}/api/${url}`, params, includeAuthToken(headers)); + }, + [put], + ), - del: useCallback(async (url, params, headers) => { - return del(`${basePath}/api/${url}`, params, headers); - }, []), + del: useCallback( + async (url, params, headers) => { + return del(`${basePath}/api/${url}`, params, includeAuthToken(headers)); + }, + [del], + ), }; } diff --git a/hooks/useRequireLogin.js b/hooks/useRequireLogin.js index 79b699e5..b00f633b 100644 --- a/hooks/useRequireLogin.js +++ b/hooks/useRequireLogin.js @@ -1,20 +1,22 @@ import { useState, useEffect } from 'react'; import { useRouter } from 'next/router'; import useUser from 'hooks/useUser'; -import { get } from 'lib/web'; +import useApi from 'hooks/useApi'; export default function useRequireLogin() { const router = useRouter(); + const { get } = useApi(); const { user, setUser } = useUser(); const [loading, setLoading] = useState(false); async function loadUser() { setLoading(true); - const { ok, data } = await get(`${router.basePath}/api/auth/verify`); + const { ok, data } = await get('/auth/verify'); if (!ok) { - return router.push('/login'); + await router.push('/login'); + return null; } setUser(data); @@ -23,12 +25,12 @@ export default function useRequireLogin() { } useEffect(() => { - if (!loading && user) { + if (loading || user) { return; } loadUser(); - }, []); + }, [user, loading]); return { user, loading }; } diff --git a/lib/middleware.js b/lib/middleware.js index c41e75d0..0c6fd081 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -3,7 +3,7 @@ import { getSession } from './session'; import { getAuthToken } from './auth'; import { unauthorized, badRequest, serverError } from './response'; -export function use(middleware) { +export function createMiddleware(middleware) { return (req, res) => new Promise((resolve, reject) => { middleware(req, res, result => { @@ -15,9 +15,9 @@ export function use(middleware) { }); } -export const useCors = use(cors()); +export const useCors = createMiddleware(cors()); -export const useSession = use(async (req, res, next) => { +export const useSession = createMiddleware(async (req, res, next) => { let session; try { @@ -35,7 +35,7 @@ export const useSession = use(async (req, res, next) => { next(); }); -export const useAuth = use(async (req, res, next) => { +export const useAuth = createMiddleware(async (req, res, next) => { const token = await getAuthToken(req); if (!token) { diff --git a/lib/web.js b/lib/web.js index f4899a2e..98fd1741 100644 --- a/lib/web.js +++ b/lib/web.js @@ -1,9 +1,6 @@ import { makeUrl } from './url'; -import { AUTH_TOKEN } from './constants'; export const apiRequest = (method, url, body, headers) => { - const authToken = getItem(AUTH_TOKEN); - return fetch(url, { method, cache: 'no-cache', @@ -11,7 +8,6 @@ export const apiRequest = (method, url, body, headers) => { headers: { Accept: 'application/json', 'Content-Type': 'application/json', - ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}), ...headers, }, body,