Skip to content

Commit

Permalink
improve login validation
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Dec 25, 2024
1 parent 968434c commit 920f0cb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/Auth/endpoints/setLoginRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function setLoginRequestHandler(this: AuthHandler, app: e.Express) {
});
}

const parseLoginData = (bodyData: any): AuthRequest.LoginData | { error: string } => {
export const parseLoginData = (bodyData: any): AuthRequest.LoginData | { error: string } => {
const loginData: AuthRequest.LoginData = {
username: "",
remember_me: !!bodyData?.remember_me,
Expand Down
28 changes: 18 additions & 10 deletions lib/Auth/endpoints/setRegisterRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AUTH_ROUTES_AND_PARAMS, HTTP_FAIL_CODES } from "../AuthHandler";
import type { AuthRegistrationConfig } from "../AuthTypes";
import { sendEmail } from "../sendEmail";
import { getClientRequestIPsInfo } from "../utils/getClientRequestIPsInfo";
import { parseLoginData } from "./setLoginRequestHandler";

type ReturnType =
| AuthResponse.MagicLinkAuthFailure
Expand All @@ -19,20 +20,26 @@ export const setRegisterRequestHandler = (
app: e.Express
) => {
const registerRequestHandler = async (req: Request, res: Response<ReturnType>) => {
const { username, password } = req.body;
const dataOrError = parseLoginData(req.body);
if ("error" in dataOrError) {
return res
.status(HTTP_FAIL_CODES.BAD_REQUEST)
.json({ success: false, code: "something-went-wrong", message: dataOrError.error });
}
const { username, password } = dataOrError;
const sendResponse = (response: ReturnType) => {
if (response.success) {
res.json(response);
} else {
res.status(HTTP_FAIL_CODES.BAD_REQUEST).json(response);
}
};
if (!username || typeof username !== "string") {
if (!username) {
return sendResponse({ success: false, code: "username-missing" });
}
if (emailAuthConfig.signupType === "withPassword") {
const { minPasswordLength = 8 } = emailAuthConfig;
if (typeof password !== "string") {
if (!password) {
return sendResponse({ success: false, code: "password-missing" });
} else if (password.length < minPasswordLength) {
return sendResponse({
Expand All @@ -47,13 +54,14 @@ export const setRegisterRequestHandler = (
const { smtp } = emailAuthConfig;
const errCodeOrResult =
emailAuthConfig.signupType === "withPassword" ?
await emailAuthConfig.onRegister({
email: username,
password,
confirmationUrlPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.confirmEmail}`,
clientInfo,
req: httpReq,
})
!password ? "weak-password"
: await emailAuthConfig.onRegister({
email: username,
password,
confirmationUrlPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.confirmEmail}`,
clientInfo,
req: httpReq,
})
: await emailAuthConfig.onRegister({
email: username,
magicLinkUrlPath: `${websiteUrl}${AUTH_ROUTES_AND_PARAMS.magicLinksRoute}`,
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.201",
"version": "4.2.202",
"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 920f0cb

Please sign in to comment.