Skip to content

Commit

Permalink
fix: cookies and redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
artgpz committed Jul 3, 2024
1 parent 71e8c37 commit 6f6cfee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { directus } from '$lib/directus'
import { verifyCookie } from '$lib/utils/cookies'
import { log } from '@arturoguzman/art-ui'
import { type Handle } from '@sveltejs/kit'

const roles = ['carer', 'healthcare-professional']
export const handle: Handle = async ({ event, resolve }) => {
// const id = getId()
// console.time(`id: ${id}`)
Expand All @@ -13,7 +13,7 @@ export const handle: Handle = async ({ event, resolve }) => {
role: event.cookies.get('etips-role') ?? 'public'
}
const role = event.cookies.get('etips-role')
if (role && !roles.includes(role)) {
if (!verifyCookie(role, ['carer', 'healthcare-professional'])) {
event.cookies.delete('etips-disclaimer-consent', { path: '/' })
event.cookies.delete('etips-role', { path: '/' })
event.cookies.delete('etips-side', { path: '/' })
Expand Down
10 changes: 8 additions & 2 deletions src/lib/ui/form/access_form.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script lang="ts">
import { enhance } from '$app/forms'
import { applyAction, enhance } from '$app/forms'
import { goto, invalidateAll } from '$app/navigation'
import { notify } from '$lib/stores/notify'
import type { Card, Page, Topic } from '$lib/types'
import Button from '$lib/ui/button/button.svelte'
import SelectionInput from '$lib/ui/form/selection_input.svelte'
import TextInput from '$lib/ui/form/text_input.svelte'
import { scrollIntoView } from '$lib/utils/scroll'
import { redirect } from '@sveltejs/kit'
import CardText from '../cards/card_text.svelte'
import DoctorFormFlow from './doctor_form_flow.svelte'
import FormDisclaimer from './form_disclaimer.svelte'
Expand Down Expand Up @@ -51,10 +52,15 @@
if (result.type === 'success') {
if (result.data?.url) {
notify.send({ value: `Welcome to eTIPS` })
await invalidateAll()
goto(`${result.data?.url}`)
await applyAction(result)
// await invalidateAll()
}
}
if (result.type === 'redirect') {
notify.send({ value: `Welcome to eTIPS` })
await applyAction(result)
}
}
}}
>
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const verifyCookie = (cookie: string | undefined, options: string[]) => {
if (!cookie) {
return false
}
if (options.includes(cookie)) return true
return false
}
30 changes: 17 additions & 13 deletions src/routes/access/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { message } from '$lib/stores/notify.js'
import type { Page } from '$lib/types/index.js'
import { createItem, readItems } from '@directus/sdk'
import { fail, redirect, type Actions } from '@sveltejs/kit'

export const load = async ({ cookies, locals }) => {
const etips_side = cookies.get('etips-side')
const etips_role = cookies.get('etips-role')
const etips_disclaimer_consent = cookies.get('etips-disclaimer-consent')
if (etips_side || etips_disclaimer_consent || etips_role) {
export const load = async ({ cookies, locals, request }) => {
const existing_cookies = [
cookies.get('etips-side'),
cookies.get('etips-role'),
cookies.get('etips-disclaimer-consent')
]
.filter((c) => c !== undefined)
.filter((c) => c !== '')
if (existing_cookies.length < 3) {
cookies.delete('etips-disclaimer-consent', { path: '/' })
cookies.delete('etips-role', { path: '/' })
cookies.delete('etips-side', { path: '/' })
}
if (existing_cookies.length === 3) {
redirect(307, '/intro')
}
const { directus } = locals
Expand Down Expand Up @@ -161,13 +166,12 @@ export const actions: Actions = {
cookies.set('etips-side', side_affected, cookies_opts)
cookies.set('etips-role', role, cookies_opts)
cookies.set('etips-disclaimer-consent', `${disclaimer_consent}`, cookies_opts)

//TODO: manual checks ready, send to directus at this point; also check redirect url so its not manually set
return {
status: 200,
message: 'ok',
url: `/intro`
}
redirect(307, '/intro')
// return {
// status: 200,
// message: 'ok',
// url: `/intro`
// }
},
'change-side': async ({ request, cookies }) => {
const form = await request.formData()
Expand Down

0 comments on commit 6f6cfee

Please sign in to comment.