Skip to content

Commit

Permalink
throw error code in auth.getUser
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 21, 2024
1 parent 44256e5 commit bc2876b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 12 deletions.
8 changes: 6 additions & 2 deletions lib/Auth/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -390,6 +390,7 @@ export class AuthHandler {
this.db,
getClientRequestIPsInfo(clientReq)
);
if (typeof clientInfo === "string") throw clientInfo;
user = clientInfo?.user;
clientUser = clientInfo?.clientUser;
}
Expand Down Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions lib/Auth/AuthTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export type SessionUser<
};

export type AuthResult<SU = SessionUser> =
| AuthFailure["code"]
| (SU & { sid: string })
| {
user?: undefined;
Expand Down
20 changes: 14 additions & 6 deletions lib/Prostgles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -406,13 +414,13 @@ export class Prostgles {
onSocketConnected = onSocketConnected.bind(this);

getClientSchema = async (clientReq: Pick<LocalParams, "socket" | "httpReq">) => {
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<ReturnType<PublishParser["getSchemaFromPublish"]>> | undefined;
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bc2876b

Please sign in to comment.