Skip to content

Commit

Permalink
fix annoying 403 issue (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabrennie committed Jun 10, 2023
1 parent cb20721 commit 6c1ceea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/hooks.server.ts
Expand Up @@ -3,6 +3,7 @@ import { GoogleProvider } from '@rabrennie/sveltekit-auth/providers';
import { JwtStrategy } from '@rabrennie/sveltekit-auth/session';
import { env } from '$env/dynamic/private';
import type { Handle, RequestEvent } from '@sveltejs/kit';
import { redirect } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import { db } from '$lib/server/database';

Expand Down Expand Up @@ -31,6 +32,16 @@ const saveUser = async (event: RequestEvent, profile: Profile) => {
...create
}
});

const redirectCookie = event.cookies.get('redirect');
if (redirectCookie) {
// prevent open redirect
const uri = new URL(redirectCookie, 'https://sprintna.me');
event.cookies.delete('redirect', { path: '/' });
if (uri.origin === 'https://sprintna.me') {
throw redirect(302, uri.pathname);
}
}
};

export const authHandlerConfig: AuthHandlerConfig = {
Expand Down
5 changes: 3 additions & 2 deletions src/routes/room/[room]/+page.server.ts
Expand Up @@ -2,7 +2,7 @@ import { db, rooms } from '$lib/server/database';
import { zk } from 'zodkit';
import type { Actions, PageServerLoad, RequestEvent } from './$types';
import { z } from 'zod';
import { error, fail } from '@sveltejs/kit';
import { error, fail, redirect } from '@sveltejs/kit';
import { roomsState } from './events/state.server';
import type Room from '../../../types/Room';
import { RoomState, type Choice as RoomChoice } from '../../../types/Room';
Expand All @@ -28,7 +28,8 @@ const mapChoice = (choice: Choice): RoomChoice => ({

export const load = (async (event) => {
if (!event.locals.user?.id) {
throw error(403, 'nope');
event.cookies.set('redirect', `/room/${getLinkId(event)}`, { path: '/', maxAge: 300 });
throw redirect(302, '/auth/redirect/google');
}

const dbRoom = await rooms.findOrError(getLinkId(event), event.locals.user.id, {
Expand Down

0 comments on commit 6c1ceea

Please sign in to comment.