break session user lookup into a util function
This commit is contained in:
parent
ec49a67418
commit
1e57731b08
19
src/lib/server/sessionutil.ts
Normal file
19
src/lib/server/sessionutil.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import {getDataSource} from './db';
|
||||
import {AuthSession} from './entity/AuthSession';
|
||||
|
||||
/** Returns the user authorized by a given session. */
|
||||
export async function getUserFromSessionID (sessionID: string | undefined) {
|
||||
const dataSource = await getDataSource();
|
||||
const sessionsRepo = dataSource.getRepository(AuthSession);
|
||||
const session = sessionID
|
||||
? await sessionsRepo.findOne({
|
||||
where: {id: sessionID},
|
||||
relations: {
|
||||
authMethod: {
|
||||
user: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
: null;
|
||||
return session?.authMethod.user;
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import {getDataSource} from '$lib/server/db';
|
||||
import {AuthSession} from '$lib/server/entity/AuthSession';
|
||||
import {getUserFromSessionID} from '$lib/server/sessionutil';
|
||||
|
||||
import type {LayoutServerLoad} from './$types';
|
||||
|
||||
|
@ -12,23 +13,7 @@ async function findSession (sessionID?: string) {
|
|||
}
|
||||
|
||||
export const load: LayoutServerLoad = async ({cookies}) => {
|
||||
const sessionID = cookies.get('sessionid');
|
||||
const dataSource = await getDataSource();
|
||||
const sessionsRepo = dataSource.getRepository(AuthSession);
|
||||
const session = sessionID
|
||||
? await sessionsRepo.findOne({
|
||||
where: {id: sessionID},
|
||||
relations: {
|
||||
authMethod: {
|
||||
user: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
: null;
|
||||
|
||||
return {
|
||||
user: session
|
||||
? JSON.parse(JSON.stringify(session.authMethod.user))
|
||||
: null,
|
||||
};
|
||||
return JSON.parse(JSON.stringify({
|
||||
user: await getUserFromSessionID(cookies.get('sessionid')),
|
||||
}));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue