mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-13 08:12:40 +10:00
completed game importing; partial work on version importing
This commit is contained in:
@ -13,52 +13,71 @@ const userSessionKey = "_userSession";
|
||||
const userIdKey = "_userId";
|
||||
|
||||
export class SessionHandler {
|
||||
private sessionProvider: SessionProvider;
|
||||
private sessionProvider: SessionProvider;
|
||||
|
||||
constructor() {
|
||||
// Create a new provider
|
||||
this.sessionProvider = createMemorySessionProvider();
|
||||
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, expend = false) {
|
||||
const result = await this.sessionProvider.updateSession(
|
||||
h3,
|
||||
userSessionKey,
|
||||
data
|
||||
);
|
||||
if (!result) {
|
||||
const toCreate = { [userSessionKey]: data };
|
||||
await this.sessionProvider.setSession(h3, toCreate, expend);
|
||||
}
|
||||
}
|
||||
async clearSession(h3: H3Event) {
|
||||
await this.sessionProvider.clearSession(h3);
|
||||
}
|
||||
|
||||
async getSession<T extends Session>(h3: H3Event) {
|
||||
const data = await this.sessionProvider.getSession<{ [userSessionKey]: T }>(h3);
|
||||
if (!data) return undefined;
|
||||
async getUserId(h3: H3Event) {
|
||||
const session = await this.sessionProvider.getSession<{
|
||||
[userIdKey]: string | undefined;
|
||||
}>(h3);
|
||||
if (!session) return undefined;
|
||||
|
||||
return data[userSessionKey];
|
||||
}
|
||||
async setSession(h3: H3Event, data: any, expend = false) {
|
||||
const result = await this.sessionProvider.updateSession(h3, userSessionKey, data);
|
||||
if (!result) {
|
||||
const toCreate = { [userSessionKey]: data };
|
||||
await this.sessionProvider.setSession(h3, toCreate, expend);
|
||||
}
|
||||
}
|
||||
async clearSession(h3: H3Event) {
|
||||
await this.sessionProvider.clearSession(h3);
|
||||
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, extend = false) {
|
||||
const result = await this.sessionProvider.updateSession(
|
||||
h3,
|
||||
userIdKey,
|
||||
userId
|
||||
);
|
||||
if (!result) {
|
||||
const toCreate = { [userIdKey]: userId };
|
||||
await this.sessionProvider.setSession(h3, toCreate, extend);
|
||||
}
|
||||
}
|
||||
|
||||
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, extend = false) {
|
||||
const result = await this.sessionProvider.updateSession(h3, userIdKey, userId);
|
||||
if (!result) {
|
||||
const toCreate = { [userIdKey]: userId };
|
||||
await this.sessionProvider.setSession(h3, toCreate, extend);
|
||||
}
|
||||
}
|
||||
async getAdminUser(h3: H3Event) {
|
||||
const user = await this.getUser(h3);
|
||||
if (!user) return undefined;
|
||||
if (!user.admin) return undefined;
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export default new SessionHandler();
|
||||
export default new SessionHandler();
|
||||
|
||||
Reference in New Issue
Block a user