break session user lookup into a util function

This commit is contained in:
Erin 2023-11-12 15:45:36 -05:00
parent ec49a67418
commit 1e57731b08
2 changed files with 23 additions and 19 deletions

View 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;
}