break session user lookup into a util function
This commit is contained in:
parent
ec49a67418
commit
1e57731b08
2 changed files with 23 additions and 19 deletions
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue