Rework pagination and cookies

This commit is contained in:
Aaron William Po
2023-03-19 18:04:13 -04:00
parent 6b12cb72c5
commit cf6a8309f1
7 changed files with 53 additions and 50 deletions

View File

@@ -3,15 +3,18 @@ import APIResponseValidationSchema from '@/validation/APIResponseValidationSchem
import useSWR from 'swr';
const useUser = () => {
// check cookies for user
const {
data: user,
error,
isLoading,
} = useSWR('/api/users/current', async (url) => {
if (!document.cookie.includes('token')) {
throw new Error('No token cookie found');
}
const response = await fetch(url);
if (!response.ok) {
document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
throw new Error(response.statusText);
}
@@ -23,6 +26,7 @@ const useUser = () => {
}
const parsedPayload = GetUserSchema.safeParse(parsed.data.payload);
console.log(parsedPayload);
if (!parsedPayload.success) {
throw new Error(parsedPayload.error.message);
}