mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { H3Event, Session } from "h3";
|
|
import createMemorySessionProvider from "./memory";
|
|
import { SessionProvider } from "./types";
|
|
import prisma from "../db/database";
|
|
|
|
/*
|
|
This is a poorly organised implemention.
|
|
|
|
It exposes an API that should stay static, but there are plenty of opportunities for optimisation/organisation under the hood
|
|
*/
|
|
|
|
const userSessionKey = "_userSession";
|
|
const userIdKey = "_userId";
|
|
|
|
export class SessionHandler {
|
|
private sessionProvider: SessionProvider;
|
|
|
|
constructor() {
|
|
// Create a new provider
|
|
this.sessionProvider = createMemorySessionProvider();
|
|
}
|
|
|
|
async getSession<T extends Session>(h3: H3Event) {
|
|
const data = await this.sessionProvider.getSession<{ [userSessionKey]: T }>(h3);
|
|
if (!data) return undefined;
|
|
|
|
return data[userSessionKey];
|
|
}
|
|
async setSession(h3: H3Event, data: any) {
|
|
const result = await this.sessionProvider.updateSession(h3, userSessionKey, data);
|
|
if (!result) {
|
|
const toCreate = { [userSessionKey]: data };
|
|
await this.sessionProvider.setSession(h3, toCreate);
|
|
}
|
|
}
|
|
async clearSession(h3: H3Event) {
|
|
await this.sessionProvider.clearSession(h3);
|
|
}
|
|
|
|
async getUserId(h3: H3Event) {
|
|
const session = await this.sessionProvider.getSession<{ [userIdKey]: string | undefined }>(h3);
|
|
if (!session) return undefined;
|
|
|
|
return session[userIdKey];
|
|
}
|
|
|
|
async getUser(h3: H3Event) {
|
|
const userId = await this.getUserId(h3);
|
|
if (!userId) return undefined;
|
|
|
|
const user = await prisma.user.findFirst({ where: { id: userId } });
|
|
return user;
|
|
}
|
|
|
|
async setUserId(h3: H3Event, userId: string) {
|
|
const result = await this.sessionProvider.updateSession(h3, userIdKey, userId);
|
|
if (!result) {
|
|
const toCreate = { [userIdKey]: userId };
|
|
await this.sessionProvider.setSession(h3, toCreate);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new SessionHandler(); |