From bc2876ba2ce2eaf2155843ebf334cc332f86528c Mon Sep 17 00:00:00 2001 From: prostgles Date: Sat, 21 Dec 2024 11:46:16 +0200 Subject: [PATCH] throw error code in auth.getUser --- lib/Auth/AuthHandler.ts | 8 ++++++-- lib/Auth/AuthTypes.ts | 1 + lib/Prostgles.ts | 20 ++++++++++++++------ package-lock.json | 4 ++-- package.json | 2 +- tests/server/package-lock.json | 2 +- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/Auth/AuthHandler.ts b/lib/Auth/AuthHandler.ts index 5a11f945..16537821 100644 --- a/lib/Auth/AuthHandler.ts +++ b/lib/Auth/AuthHandler.ts @@ -358,7 +358,7 @@ export class AuthHandler { const isSocket = "socket" in localParams; if (isSocket && getSession && localParams.socket?.__prglCache) { const { session, user, clientUser } = localParams.socket.__prglCache; - const isValid = this.isValidSocketSession(localParams.socket, session); + const isValid = this.isNonExpiredSocketSession(localParams.socket, session); if (isValid) { return { sid: session.sid, @@ -390,6 +390,7 @@ export class AuthHandler { this.db, getClientRequestIPsInfo(clientReq) ); + if (typeof clientInfo === "string") throw clientInfo; user = clientInfo?.user; clientUser = clientInfo?.clientUser; } @@ -421,7 +422,10 @@ export class AuthHandler { return clientInfo; } - isValidSocketSession = (socket: PRGLIOSocket, session: BasicSession | undefined): boolean => { + isNonExpiredSocketSession = ( + socket: PRGLIOSocket, + session: BasicSession | undefined + ): boolean => { const hasExpired = Boolean(session && session.expires <= Date.now()); if (this.opts.expressConfig?.publicRoutes && !this.opts.expressConfig.disableSocketAuthGuard) { const error = "Session has expired"; diff --git a/lib/Auth/AuthTypes.ts b/lib/Auth/AuthTypes.ts index 120099e5..21b29885 100644 --- a/lib/Auth/AuthTypes.ts +++ b/lib/Auth/AuthTypes.ts @@ -240,6 +240,7 @@ export type SessionUser< }; export type AuthResult = + | AuthFailure["code"] | (SU & { sid: string }) | { user?: undefined; diff --git a/lib/Prostgles.ts b/lib/Prostgles.ts index 48d70a44..895e4309 100644 --- a/lib/Prostgles.ts +++ b/lib/Prostgles.ts @@ -27,7 +27,15 @@ import { export { DBHandlerServer }; export type PGP = pgPromise.IMain<{}, pg.IClient>; -import { CHANNELS, ClientSchema, SQLRequest, isObject, omitKeys, tryCatch } from "prostgles-types"; +import { + CHANNELS, + ClientSchema, + SQLRequest, + isObject, + omitKeys, + tryCatch, + tryCatchV2, +} from "prostgles-types"; import { DBEventsManager } from "./DBEventsManager"; import { PublishParser } from "./PublishParser/PublishParser"; export { getOrSetTransporter, sendEmail, verifySMTPConfig } from "./Auth/sendEmail"; @@ -406,13 +414,13 @@ export class Prostgles { onSocketConnected = onSocketConnected.bind(this); getClientSchema = async (clientReq: Pick) => { - const result = await tryCatch(async () => { + const result = await tryCatchV2(async () => { const clientInfo = clientReq.socket ? { type: "socket" as const, socket: clientReq.socket } : clientReq.httpReq ? { type: "http" as const, httpReq: clientReq.httpReq } : undefined; if (!clientInfo) throw "Invalid client"; - // if (!this.authHandler) throw "this.authHandler missing"; + const userData = await this.authHandler?.getClientInfo(clientInfo); const { publishParser } = this; let fullSchema: Awaited> | undefined; @@ -493,16 +501,16 @@ export class Prostgles { userData, }; }); - const sid = result.userData?.sid ?? this.authHandler?.getSIDNoError(clientReq); + const sid = result.data?.userData?.sid ?? this.authHandler?.getSIDNoError(clientReq); await this.opts.onLog?.({ type: "connect.getClientSchema", duration: result.duration, sid, socketId: clientReq.socket?.id, - error: result.error || result.publishValidationError, + error: result.error || result.data?.publishValidationError, }); if (result.hasError) throw result.error; - return result.clientSchema; + return result.data.clientSchema; }; pushSocketSchema = async (socket: PRGLIOSocket) => { diff --git a/package-lock.json b/package-lock.json index 71814e4a..63b3c6ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "prostgles-server", - "version": "4.2.190", + "version": "4.2.191", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "prostgles-server", - "version": "4.2.190", + "version": "4.2.191", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0", diff --git a/package.json b/package.json index f1566853..0c911243 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prostgles-server", - "version": "4.2.190", + "version": "4.2.191", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/tests/server/package-lock.json b/tests/server/package-lock.json index 1499bb13..ca193beb 100644 --- a/tests/server/package-lock.json +++ b/tests/server/package-lock.json @@ -21,7 +21,7 @@ }, "../..": { "name": "prostgles-server", - "version": "4.2.190", + "version": "4.2.191", "license": "MIT", "dependencies": { "@aws-sdk/client-ses": "^3.699.0",