mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-24 00:42:51 +10:00
ca groundwork
This commit is contained in:
@@ -26,11 +26,11 @@ export class SessionHandler {
|
||||
|
||||
return data[userSessionKey];
|
||||
}
|
||||
async setSession(h3: H3Event, data: any) {
|
||||
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);
|
||||
await this.sessionProvider.setSession(h3, toCreate, expend);
|
||||
}
|
||||
}
|
||||
async clearSession(h3: H3Event) {
|
||||
@@ -52,11 +52,11 @@ export class SessionHandler {
|
||||
return user;
|
||||
}
|
||||
|
||||
async setUserId(h3: H3Event, userId: string) {
|
||||
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);
|
||||
await this.sessionProvider.setSession(h3, toCreate, extend);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
import moment from "moment";
|
||||
import { Session, SessionProvider } from "./types";
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export default function createMemorySessionHandler() {
|
||||
const sessions: { [key: string]: Session } = {}
|
||||
const sessions: { [key: string]: Session } = {};
|
||||
|
||||
const sessionCookieName = "drop-session";
|
||||
const sessionCookieName = "drop-session";
|
||||
|
||||
const memoryProvider: SessionProvider = {
|
||||
async setSession(h3, data) {
|
||||
const existingCookie = getCookie(h3, sessionCookieName);
|
||||
if (existingCookie) delete sessions[existingCookie]; // Clear any previous session
|
||||
const memoryProvider: SessionProvider = {
|
||||
async setSession(h3, data, extend = false) {
|
||||
const existingCookie = getCookie(h3, sessionCookieName);
|
||||
if (existingCookie) delete sessions[existingCookie]; // Clear any previous session
|
||||
|
||||
const cookie = uuidv4();
|
||||
const expiry = moment().add(31, 'day');
|
||||
setCookie(h3, sessionCookieName, cookie, { expires: expiry.toDate() });
|
||||
const cookie = uuidv4();
|
||||
const expiry = moment().add(31, extend ? "month" : "day");
|
||||
setCookie(h3, sessionCookieName, cookie, { expires: expiry.toDate() });
|
||||
|
||||
sessions[cookie] = data;
|
||||
return true;
|
||||
},
|
||||
async updateSession(h3, key, data) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return false;
|
||||
sessions[cookie] = data;
|
||||
return true;
|
||||
},
|
||||
async updateSession(h3, key, data) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return false;
|
||||
|
||||
sessions[cookie] = Object.assign({}, sessions[cookie], { [key]: data });
|
||||
return true;
|
||||
},
|
||||
async getSession(h3) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return undefined;
|
||||
sessions[cookie] = Object.assign({}, sessions[cookie], { [key]: data });
|
||||
return true;
|
||||
},
|
||||
async getSession(h3) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return undefined;
|
||||
|
||||
return sessions[cookie] as any; // Wild type cast because we let the user specify types if they want
|
||||
},
|
||||
async clearSession(h3) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return;
|
||||
return sessions[cookie] as any; // Wild type cast because we let the user specify types if they want
|
||||
},
|
||||
async clearSession(h3) {
|
||||
const cookie = getCookie(h3, sessionCookieName);
|
||||
if (!cookie) return;
|
||||
|
||||
delete sessions[cookie];
|
||||
deleteCookie(h3, sessionCookieName);
|
||||
},
|
||||
};
|
||||
delete sessions[cookie];
|
||||
deleteCookie(h3, sessionCookieName);
|
||||
},
|
||||
};
|
||||
|
||||
return memoryProvider;
|
||||
}
|
||||
return memoryProvider;
|
||||
}
|
||||
|
||||
Vendored
+9
-5
@@ -3,8 +3,12 @@ import { H3Event } from "h3";
|
||||
export type Session = { [key: string]: any };
|
||||
|
||||
export interface SessionProvider {
|
||||
setSession: (h3: H3Event, data: Session) => Promise<boolean>;
|
||||
updateSession: (h3: H3Event, key: string, data: any) => Promise<boolean>;
|
||||
getSession: <T extends Session>(h3: H3Event) => Promise<T | undefined>;
|
||||
clearSession: (h3: H3Event) => Promise<void>;
|
||||
}
|
||||
setSession: (
|
||||
h3: H3Event,
|
||||
data: Session,
|
||||
extend?: boolean
|
||||
) => Promise<boolean>;
|
||||
updateSession: (h3: H3Event, key: string, data: any) => Promise<boolean>;
|
||||
getSession: <T extends Session>(h3: H3Event) => Promise<T | undefined>;
|
||||
clearSession: (h3: H3Event) => Promise<void>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user