mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-14 08:41:15 +10:00
feat(sessions): cleanup and raw accessors
This commit is contained in:
@ -71,24 +71,27 @@ export class SessionHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUserId(h3: H3Event, tag: string | boolean = false) {
|
async getUserId(h3: H3Event) {
|
||||||
const token = this.getSessionToken(h3);
|
const token = this.getSessionToken(h3);
|
||||||
if (!token) return undefined;
|
if (!token) return undefined;
|
||||||
|
|
||||||
|
return await this.getUserIdRaw(token);
|
||||||
|
}
|
||||||
|
async getUserIdRaw(token: string) {
|
||||||
const session = await this.sessionProvider.getSession<{
|
const session = await this.sessionProvider.getSession<{
|
||||||
[userIdKey]: string | undefined;
|
[userIdKey]: string | undefined;
|
||||||
}>(token);
|
}>(token);
|
||||||
|
|
||||||
if (tag)
|
|
||||||
console.log(`${tag} ${JSON.stringify(h3)} ${JSON.stringify(session)}`);
|
|
||||||
|
|
||||||
if (!session) return undefined;
|
if (!session) return undefined;
|
||||||
|
|
||||||
return session[userIdKey];
|
return session[userIdKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUser(h3: H3Event) {
|
async getUser(obj: H3Event | string) {
|
||||||
const userId = await this.getUserId(h3);
|
const userId =
|
||||||
|
typeof obj === "string"
|
||||||
|
? await this.getUserIdRaw(obj)
|
||||||
|
: await this.getUserId(obj);
|
||||||
if (!userId) return undefined;
|
if (!userId) return undefined;
|
||||||
|
|
||||||
const user = await prisma.user.findFirst({ where: { id: userId } });
|
const user = await prisma.user.findFirst({ where: { id: userId } });
|
||||||
@ -106,7 +109,7 @@ export class SessionHandler {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAdminUser(h3: H3Event) {
|
async getAdminUser(h3: H3Event | string) {
|
||||||
const user = await this.getUser(h3);
|
const user = await this.getUser(h3);
|
||||||
if (!user) return undefined;
|
if (!user) return undefined;
|
||||||
if (!user.admin) return undefined;
|
if (!user.admin) return undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user