Skip to content

Commit

Permalink
return auth errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Nov 27, 2024
1 parent 5481b9a commit 3f1cf3a
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/Auth/AuthHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const HTTPCODES = {
NOT_FOUND: 404,
BAD_REQUEST: 400,
INTERNAL_SERVER_ERROR: 500,
};
} as const;

export const getLoginClientInfo = (req: AuthClientRequest): AuthClientRequest & LoginClientInfo => {
if("httpReq" in req){
Expand Down
12 changes: 6 additions & 6 deletions lib/Auth/setEmailProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import e from "express";
import { AUTH_ROUTES_AND_PARAMS, AuthHandler } from "./AuthHandler";
import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTPCODES } from "./AuthHandler";
import { Email, SMTPConfig } from "./AuthTypes";
import { sendEmail } from "./sendEmail";
import { promises } from "node:dns";
Expand Down Expand Up @@ -27,7 +27,7 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
}
}
if(validationError){
res.status(400).json({ error: validationError });
res.status(HTTPCODES.AUTH_ERROR).json({ success: false, error: validationError });
return;
}
try {
Expand All @@ -46,10 +46,10 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {

if(emailMessage){
await sendEmail(emailMessage.smtp, emailMessage.message);
res.json({ msg: "Email sent" });
res.json({ success: true, message: "Email sent" });
}
} catch {
res.status(500).json({ error: "Failed to send email" });
res.status(HTTPCODES.AUTH_ERROR).json({ success: false, error: "Failed to send email" });
}
});

Expand All @@ -58,9 +58,9 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
const { id } = req.params ?? {};
try {
await email.emailConfirmation?.onConfirmed({ confirmationCode: id });
res.json({ msg: "Email confirmed" });
res.json({ success: true, message: "Email confirmed" });
} catch (_e) {
res.status(500).json({ error: "Failed to confirm email" });
res.status(HTTPCODES.AUTH_ERROR).json({ success: false, error: "Failed to confirm email" });
}
});
}
Expand Down
14 changes: 6 additions & 8 deletions lib/Auth/setupAuthRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export async function setupAuthRoutes(this: AuthHandler) {
const session = await this.throttledFunc(async () => {
return check(id, this.dbo as any, this.db, getLoginClientInfo({ httpReq: req }));
});
if (!session) {
res.status(HTTPCODES.AUTH_ERROR).json({ msg: "Invalid magic-link" });
if(!session) {
res.status(HTTPCODES.AUTH_ERROR).json({ error: "Invalid magic-link" });
} else {
this.setCookieAndGoToReturnURLIFSet(session, { req, res });
}

} catch (e) {
res.status(HTTPCODES.AUTH_ERROR).json({ msg: e });
res.status(HTTPCODES.AUTH_ERROR).json({ error: e });
}
}
});
Expand All @@ -80,11 +80,9 @@ export async function setupAuthRoutes(this: AuthHandler) {
};

await this.loginThrottledAndSetCookie(req, res, loginParams);
} catch (err) {
console.log(err)
res.status(HTTPCODES.AUTH_ERROR).json({ err });
} catch (error) {
res.status(HTTPCODES.AUTH_ERROR).json({ error });
}

});

const onLogout = async (req: ExpressReq, res: ExpressRes) => {
Expand Down Expand Up @@ -154,7 +152,7 @@ export async function setupAuthRoutes(this: AuthHandler) {
} catch (error) {
console.error(error);
const errorMessage = typeof error === "string" ? error : error instanceof Error ? error.message : "";
res.status(HTTPCODES.AUTH_ERROR).json({ msg: "Something went wrong when processing your request" + (errorMessage? (": " + errorMessage) : "") });
res.status(HTTPCODES.AUTH_ERROR).json({ error: "Something went wrong when processing your request" + (errorMessage? (": " + errorMessage) : "") });
}

});
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.160",
"version": "4.2.161",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions tests/client/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 tests/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"dependencies": {
"@types/node": "^20.9.2",
"prostgles-client": "^4.0.150",
"prostgles-client": "^4.0.154",
"prostgles-types": "^4.0.51",
"socket.io-client": "^4.7.5"
},
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 3f1cf3a

Please sign in to comment.