reference cookie names from an enum

This commit is contained in:
Erin 2023-11-12 16:13:51 -05:00
parent 9f89ba231a
commit ec543afcb2
5 changed files with 18 additions and 10 deletions

View file

@ -0,0 +1,5 @@
/** Enumeration of cookies which may be set */
export enum Cookie {
SESSION_ID = 'sessionid',
STATE_ID = 'stateid',
}

View file

@ -1,5 +1,4 @@
import {getDataSource} from '$lib/server/db'; import {Cookie} from '$lib/server/cookies';
import {AuthSession} from '$lib/server/entity/AuthSession';
import {getUserFromSessionID} from '$lib/server/sessionutil'; import {getUserFromSessionID} from '$lib/server/sessionutil';
import type {LayoutServerLoad} from './$types'; import type {LayoutServerLoad} from './$types';
@ -14,6 +13,6 @@ async function findSession (sessionID?: string) {
export const load: LayoutServerLoad = async ({cookies}) => { export const load: LayoutServerLoad = async ({cookies}) => {
return JSON.parse(JSON.stringify({ return JSON.parse(JSON.stringify({
user: await getUserFromSessionID(cookies.get('sessionid')), user: await getUserFromSessionID(cookies.get(Cookie.SESSION_ID)),
})); }));
}; };

View file

@ -10,6 +10,7 @@ import {
type AuthProviderImplementation, type AuthProviderImplementation,
authProviderImplementations, authProviderImplementations,
} from '$lib/server/auth'; } from '$lib/server/auth';
import {Cookie} from '$lib/server/cookies';
import type {PageServerLoad} from './$types'; import type {PageServerLoad} from './$types';
export const load: PageServerLoad = async ({cookies, params, url}) => { export const load: PageServerLoad = async ({cookies, params, url}) => {
@ -35,7 +36,7 @@ export const load: PageServerLoad = async ({cookies, params, url}) => {
await authStatesRepo.save(state); await authStatesRepo.save(state);
// set the state ID as a cookie so we can retrieve it later and compare // set the state ID as a cookie so we can retrieve it later and compare
cookies.set('stateid', state.id, {path: '/auth/discord'}); cookies.set(Cookie.STATE_ID, state.id, {path: '/auth/discord'});
// redirect to the provider with the state // redirect to the provider with the state
throw redirect(302, providerImpl.buildAuthURI(state.state)); throw redirect(302, providerImpl.buildAuthURI(state.state));

View file

@ -5,6 +5,7 @@ import {redirect} from '@sveltejs/kit';
import type {PageServerLoad} from './$types'; import type {PageServerLoad} from './$types';
import {AuthProvider, authProviderImplementations} from '$lib/server/auth'; import {AuthProvider, authProviderImplementations} from '$lib/server/auth';
import {Cookie} from '$lib/server/cookies';
import {AuthMethod} from '$lib/server/entity/AuthMethod'; import {AuthMethod} from '$lib/server/entity/AuthMethod';
import {User} from '$lib/server/entity/User'; import {User} from '$lib/server/entity/User';
@ -23,7 +24,7 @@ export const load: PageServerLoad = async event => {
const dataSource = await getDataSource(); const dataSource = await getDataSource();
const statesRepo = dataSource.getRepository(AuthState); const statesRepo = dataSource.getRepository(AuthState);
const stateID = event.cookies.get('stateid'); const stateID = event.cookies.get(Cookie.STATE_ID);
let storedState: AuthState | null = null; let storedState: AuthState | null = null;
if (stateID) { if (stateID) {
storedState = await statesRepo.findOne({where: {id: stateID}}); storedState = await statesRepo.findOne({where: {id: stateID}});
@ -103,11 +104,11 @@ export const load: PageServerLoad = async event => {
const authSessionRepo = dataSource.getRepository(AuthSession); const authSessionRepo = dataSource.getRepository(AuthSession);
const authSession = authSessionRepo.create({authMethod}); const authSession = authSessionRepo.create({authMethod});
await authSessionRepo.save(authSession); await authSessionRepo.save(authSession);
event.cookies.set('sessionid', authSession.id, {path: '/'}); event.cookies.set(Cookie.SESSION_ID, authSession.id, {path: '/'});
// remove the state we were using now that we're done with it // remove the state we were using now that we're done with it
await statesRepo.remove(storedState); await statesRepo.remove(storedState);
event.cookies.delete('stateid'); event.cookies.delete(Cookie.STATE_ID);
// Woo we did it, redirect on to wherever we were trying to go before // Woo we did it, redirect on to wherever we were trying to go before
let next = storedState.next; let next = storedState.next;

View file

@ -1,16 +1,18 @@
import {type Actions, redirect} from '@sveltejs/kit';
import {Cookie} from '$lib/server/cookies';
import {getDataSource} from '$lib/server/db'; import {getDataSource} from '$lib/server/db';
import {User} from '$lib/server/entity/User'; import {User} from '$lib/server/entity/User';
import {getUserFromSessionID} from '$lib/server/sessionutil'; import {getUserFromSessionID} from '$lib/server/sessionutil';
import {type Actions, redirect} from '@sveltejs/kit';
export const actions = { export const actions = {
async delete ({cookies}) { async delete ({cookies}) {
const user = await getUserFromSessionID(cookies.get('sessionid')); const user = await getUserFromSessionID(cookies.get(Cookie.SESSION_ID));
if (user) { if (user) {
const dataSource = await getDataSource(); const dataSource = await getDataSource();
const usersRepo = dataSource.getRepository(User); const usersRepo = dataSource.getRepository(User);
usersRepo.remove(user); usersRepo.remove(user);
cookies.delete('sessionid'); cookies.delete(Cookie.SESSION_ID);
} }
throw redirect(302, '/'); throw redirect(302, '/');