This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
317 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const authenticatedRoutes: string[] = ['/games/proposals']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { redirect } from 'sveltekit-flash-message/server'; | ||
|
||
import { authenticatedRoutes } from './authenticated_routes'; | ||
|
||
import type { Handle } from '@sveltejs/kit'; | ||
|
||
export default (async ({ event, resolve }) => { | ||
// check url against list of routes that require authentication | ||
if (authenticatedRoutes.includes(event.url.pathname)) { | ||
// check if user is logged in | ||
const session = await event.locals.getSession(); | ||
if (!session) { | ||
// user is not logged in, redirect to login page | ||
throw redirect( | ||
303, | ||
'/auth', | ||
{ type: 'error', message: 'You must be logged in to view this page.' }, | ||
event | ||
); | ||
} | ||
} | ||
|
||
return resolve(event); | ||
}) satisfies Handle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { loadFlash } from 'sveltekit-flash-message/server'; | ||
|
||
import type { LayoutServerLoad } from './$types'; | ||
|
||
export const load: LayoutServerLoad = async ({ locals: { getSession } }) => { | ||
export const load: LayoutServerLoad = loadFlash(async ({ locals: { getSession } }) => { | ||
return { | ||
session: await getSession() | ||
}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,44 @@ | ||
import { fail } from '@sveltejs/kit'; | ||
import { setFlash } from 'sveltekit-flash-message/server'; | ||
|
||
import { superValidate } from 'sveltekit-superforms/server'; | ||
|
||
import { z } from 'zod'; | ||
|
||
const schema = z.object({ | ||
name: z.string().default('John') | ||
import type { Actions } from './$types'; | ||
|
||
const formSchema = z.object({ | ||
game_id: z.number(), | ||
vote: z.enum(['upvote', 'downvote']) | ||
}); | ||
|
||
export const load = async () => { | ||
const form = await superValidate(schema); | ||
export const actions: Actions = { | ||
default: async (event) => { | ||
const form = await superValidate(event, formSchema); | ||
if (!form.valid) { | ||
return fail(400, { form }); | ||
} | ||
|
||
const session = await event.locals.getSession(); | ||
if (!session) { | ||
setFlash({ type: 'error', message: 'You must be logged in to vote.' }, event); | ||
return fail(401); | ||
} | ||
const { | ||
user: { id: user_id } | ||
} = session; | ||
|
||
const { error } = await event.locals.supabase.from('game_votes').upsert({ | ||
game_id: form.data.game_id, | ||
voter_id: user_id, | ||
is_upvote: form.data.vote === 'upvote' | ||
}); | ||
|
||
if (error) { | ||
setFlash({ type: 'error', message: 'There was an error saving your vote.' }, event); | ||
return fail(400, { form }); | ||
} | ||
|
||
return { form }; | ||
setFlash({ type: 'success', message: 'Your vote has been noted.' }, event); | ||
} | ||
}; |
Oops, something went wrong.