From b7225ad5503f3438a0cae07ea613754b5585c49c Mon Sep 17 00:00:00 2001 From: EETagent Date: Sat, 3 Dec 2022 16:41:43 +0100 Subject: [PATCH] feat: ssr fetch details --- frontend/src/hooks.server.ts | 2 ++ frontend/src/lib/@api/candidate.ts | 46 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/frontend/src/hooks.server.ts b/frontend/src/hooks.server.ts index 457c7db..0777634 100644 --- a/frontend/src/hooks.server.ts +++ b/frontend/src/hooks.server.ts @@ -5,6 +5,8 @@ export const handleFetch: HandleFetch = async ({ request, fetch, event,}) => { const cookie = event.request.headers.get('cookie') || ''; + console.log(`SSR: handleFetch() cookie: ${cookie}`); + request.headers.set('cookie', cookie); request.headers.append('Origin', event.url.origin); diff --git a/frontend/src/lib/@api/candidate.ts b/frontend/src/lib/@api/candidate.ts index fc7b58b..f478d47 100644 --- a/frontend/src/lib/@api/candidate.ts +++ b/frontend/src/lib/@api/candidate.ts @@ -3,6 +3,35 @@ import type { CandidateData, CandidateLogin } from '$lib/stores/candidate'; import type { SubmissionProgress } from '$lib/stores/portfolio'; import { API_URL, errorHandler } from '.'; + +// SSR Compatible +export const apiLogout = async (fetchSsr?: any) => { + try { + fetchSsr ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) : await axios.post(API_URL + '/candidate/logout', { withCredentials: true }); + } catch (e: any) { + throw errorHandler(e, 'Logout failed'); + } +} + +// SSR Compatible +export const apiFetchDetails = async (fetchSsr?: any): Promise => { + try { + if (fetchSsr) { + const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' }); + const body = await res.text(); + console.log(body); + if (res.status === 500) { + return null; + } + return JSON.parse(body); + } + const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true }); + return res.data; + } catch (e: any) { + throw errorHandler(e, 'Failed to fill details'); + } +} + export const apiWhoami = async (): Promise => { try { const res = await axios.get(`${API_URL}/whoami`); @@ -21,14 +50,6 @@ export const apiLogin = async (data: CandidateLogin): Promise => { } } -// TODO -export const apiLogout = async (fetchSsr?: any) => { - try { - fetchSsr ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) : await axios.post(API_URL + '/candidate/logout', { withCredentials: true }); - } catch (e: any) { - throw errorHandler(e, 'Logout failed'); - } -} export const apiFillDetails = async (data: CandidateData): Promise => { console.log(data); @@ -40,15 +61,6 @@ export const apiFillDetails = async (data: CandidateData): Promise => { - try { - const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true }); - return res.data; - } catch (e: any) { - throw errorHandler(e, 'Failed to fill details'); - } -} - export const apiFetchSubmissionProgress = async (): Promise => { try { const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', { withCredentials: true });