diff --git a/frontend/composables/axios.ts b/frontend/composables/axios.ts deleted file mode 100644 index 9b997d3..0000000 --- a/frontend/composables/axios.ts +++ /dev/null @@ -1,8 +0,0 @@ -import axios from 'axios' - -const request = axios.create({ - baseURL: 'http://localhost:8080', - withCredentials: true, -}) - -export { request } diff --git a/frontend/composables/fetch.ts b/frontend/composables/fetch.ts new file mode 100644 index 0000000..612f2fa --- /dev/null +++ b/frontend/composables/fetch.ts @@ -0,0 +1,25 @@ +import type { UseFetchOptions } from 'nuxt/app' + +const useCustomFetch = async ( + path: string, + options: UseFetchOptions = {} +) => { + let headers: any = {} + const token = useCookie('XSRF-TOKEN') + + if (token.value) { + headers['X-XSRF-TOKEN'] = token.value as string + } + + return useFetch('http://localhost:8080' + path, { + credentials: 'include', + watch: false, + ...options, + headers: { + ...headers, + ...options?.headers, + }, + }) +} + +export { useCustomFetch } diff --git a/frontend/composables/post.ts b/frontend/composables/post.ts index 56e46c4..14d0109 100644 --- a/frontend/composables/post.ts +++ b/frontend/composables/post.ts @@ -33,7 +33,10 @@ const createNewPost = async (data: unknown) => { if (!parsedNewPost.success) return - const res = await request.post('/api/posts', parsedNewPost.data) + const { data: post } = await useCustomFetch('/api/posts', { + method: 'POST', + body: parsedNewPost.data, + }) await fetchPosts() } @@ -51,11 +54,11 @@ const fetchPosts = async (refresh: Boolean = false) => { ? posts.value[posts.value.length - 1].sequence : 0 - const data = await request.get('/api/posts', { + const { data } = await useCustomFetch('/api/posts', { params: { lastPost: lastPost }, }) - const response = responseValidator.safeParse(data.data) + const response = responseValidator.safeParse(data.value) if (!response.success) return @@ -63,10 +66,10 @@ const fetchPosts = async (refresh: Boolean = false) => { } export { + createNewPost, + fetchPosts, parsePost, parsePosts, PostValidator, - createNewPost, usePosts, - fetchPosts, } diff --git a/frontend/composables/user.ts b/frontend/composables/user.ts index 44db735..201ae5b 100644 --- a/frontend/composables/user.ts +++ b/frontend/composables/user.ts @@ -17,8 +17,8 @@ const userLogin = async (email: String, password: String) => { if (!user.success) return user.error.format() - await request.get('/sanctum/csrf-cookie') - await request.post('/login', user.data) + await useCustomFetch('/sanctum/csrf-cookie') + await useCustomFetch('/login', { method: 'POST', body: user.data }) await getUser() } @@ -35,29 +35,26 @@ const userSignup = async (data: unknown) => { if (!newUser.success) return newUser.error.format() - await request.post('/register', newUser.data) + await useCustomFetch('/register', { method: 'POST', body: newUser.data }) await getUser() } const getUser = async () => { - await request - .get('/api/user') - .then(({ data }) => { - const parsed = loggedInUserSchema.safeParse(data) - if (!parsed.success) return - useUser().value = parsed.data.name - }) - .catch((error) => { - console.log(error) - }) + const { data, error } = await useCustomFetch('/api/user') + + if (error.value) return + + const parsed = loggedInUserSchema.safeParse(data.value) + if (!parsed.success) return + useUser().value = parsed.data.name } const userLogout = async () => { - await request.post('/logout') + await useCustomFetch('/logout', { method: 'POST' }) useUser().value = '' } const useUser = () => useState('user', () => '') -export { userLogin, useUser, getUser, userLogout, userSignup } +export { getUser, userLogin, userLogout, userSignup, useUser }