reference cookie names from an enum
This commit is contained in:
parent
9f89ba231a
commit
ec543afcb2
5
src/lib/server/cookies.ts
Normal file
5
src/lib/server/cookies.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
/** Enumeration of cookies which may be set */
|
||||
export enum Cookie {
|
||||
SESSION_ID = 'sessionid',
|
||||
STATE_ID = 'stateid',
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
import {getDataSource} from '$lib/server/db';
|
||||
import {AuthSession} from '$lib/server/entity/AuthSession';
|
||||
import {Cookie} from '$lib/server/cookies';
|
||||
import {getUserFromSessionID} from '$lib/server/sessionutil';
|
||||
|
||||
import type {LayoutServerLoad} from './$types';
|
||||
|
@ -14,6 +13,6 @@ async function findSession (sessionID?: string) {
|
|||
|
||||
export const load: LayoutServerLoad = async ({cookies}) => {
|
||||
return JSON.parse(JSON.stringify({
|
||||
user: await getUserFromSessionID(cookies.get('sessionid')),
|
||||
user: await getUserFromSessionID(cookies.get(Cookie.SESSION_ID)),
|
||||
}));
|
||||
};
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
type AuthProviderImplementation,
|
||||
authProviderImplementations,
|
||||
} from '$lib/server/auth';
|
||||
import {Cookie} from '$lib/server/cookies';
|
||||
import type {PageServerLoad} from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({cookies, params, url}) => {
|
||||
|
@ -35,7 +36,7 @@ export const load: PageServerLoad = async ({cookies, params, url}) => {
|
|||
await authStatesRepo.save(state);
|
||||
|
||||
// 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
|
||||
throw redirect(302, providerImpl.buildAuthURI(state.state));
|
||||
|
|
|
@ -5,6 +5,7 @@ import {redirect} from '@sveltejs/kit';
|
|||
import type {PageServerLoad} from './$types';
|
||||
|
||||
import {AuthProvider, authProviderImplementations} from '$lib/server/auth';
|
||||
import {Cookie} from '$lib/server/cookies';
|
||||
import {AuthMethod} from '$lib/server/entity/AuthMethod';
|
||||
import {User} from '$lib/server/entity/User';
|
||||
|
||||
|
@ -23,7 +24,7 @@ export const load: PageServerLoad = async event => {
|
|||
const dataSource = await getDataSource();
|
||||
const statesRepo = dataSource.getRepository(AuthState);
|
||||
|
||||
const stateID = event.cookies.get('stateid');
|
||||
const stateID = event.cookies.get(Cookie.STATE_ID);
|
||||
let storedState: AuthState | null = null;
|
||||
if (stateID) {
|
||||
storedState = await statesRepo.findOne({where: {id: stateID}});
|
||||
|
@ -103,11 +104,11 @@ export const load: PageServerLoad = async event => {
|
|||
const authSessionRepo = dataSource.getRepository(AuthSession);
|
||||
const authSession = authSessionRepo.create({authMethod});
|
||||
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
|
||||
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
|
||||
let next = storedState.next;
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
import {type Actions, redirect} from '@sveltejs/kit';
|
||||
|
||||
import {Cookie} from '$lib/server/cookies';
|
||||
import {getDataSource} from '$lib/server/db';
|
||||
import {User} from '$lib/server/entity/User';
|
||||
import {getUserFromSessionID} from '$lib/server/sessionutil';
|
||||
import {type Actions, redirect} from '@sveltejs/kit';
|
||||
|
||||
export const actions = {
|
||||
async delete ({cookies}) {
|
||||
const user = await getUserFromSessionID(cookies.get('sessionid'));
|
||||
const user = await getUserFromSessionID(cookies.get(Cookie.SESSION_ID));
|
||||
if (user) {
|
||||
const dataSource = await getDataSource();
|
||||
const usersRepo = dataSource.getRepository(User);
|
||||
usersRepo.remove(user);
|
||||
cookies.delete('sessionid');
|
||||
cookies.delete(Cookie.SESSION_ID);
|
||||
}
|
||||
|
||||
throw redirect(302, '/');
|
||||
|
|
Loading…
Reference in a new issue