Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sevelinCa committed Jul 21, 2024
1 parent e99cd05 commit 6d90e72
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
136 changes: 68 additions & 68 deletions src/middleware/2fa.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
import { Request, Response, NextFunction } from 'express';
import { Session } from 'express-session';
import { generate2FACode, verify2FACode } from '../services/2fa.service';
// import { Request, Response, NextFunction } from 'express';
// import { Session } from 'express-session';
// import { generate2FACode, verify2FACode } from '../services/2fa.service';

interface ExtendedSession extends Session {
email?: string;
password?: string;
twoFactorCode?: string | null;
twoFactorExpiry?: Date | null;
twoFAError?: string;
}
// interface ExtendedSession extends Session {
// email?: string;
// password?: string;
// twoFactorCode?: string | null;
// twoFactorExpiry?: Date | null;
// twoFAError?: string;
// }

interface ExtendedRequest extends Request {
session: ExtendedSession;
}
// interface ExtendedRequest extends Request {
// session: ExtendedSession;
// }

export const twoFAController = async (
req: ExtendedRequest,
res: Response,
next: NextFunction
) => {
const { email, password } = req.body;
const twoFactorData = await generate2FACode(req.body);
const extSession = req.session;
// export const twoFAController = async (
// req: ExtendedRequest,
// res: Response,
// next: NextFunction
// ) => {
// const { email, password } = req.body;
// const twoFactorData = await generate2FACode(req.body);
// const extSession = req.session;

if (twoFactorData) {
extSession.twoFactorCode = twoFactorData.twoFactorCode;
if (typeof twoFactorData.twoFactorExpiry === 'number') {
extSession.twoFactorExpiry = new Date(twoFactorData.twoFactorExpiry);
}
extSession.email = email;
extSession.password = password;
return res.status(200).json({ message: '2FA code sent. Please verify the code.' });
} else {
next();
}
};
// if (twoFactorData) {
// extSession.twoFactorCode = twoFactorData.twoFactorCode;
// if (typeof twoFactorData.twoFactorExpiry === 'number') {
// extSession.twoFactorExpiry = new Date(twoFactorData.twoFactorExpiry);
// }
// extSession.email = email;
// extSession.password = password;
// return res.status(200).json({ message: '2FA code sent. Please verify the code.' });
// } else {
// next();
// }
// };

export const verifyCode = async (
req: ExtendedRequest,
res: Response,
next: NextFunction
) => {
const extendedSession = req.session;
const { code } = req.body;
// export const verifyCode = async (
// req: ExtendedRequest,
// res: Response,
// next: NextFunction
// ) => {
// const extendedSession = req.session;
// const { code } = req.body;

const sessionCode = extendedSession.twoFactorCode;
const sessionExpiry = extendedSession.twoFactorExpiry;
// const sessionCode = extendedSession.twoFactorCode;
// const sessionExpiry = extendedSession.twoFactorExpiry;

if (sessionCode && sessionExpiry) {
const sessionExpiryDate = new Date(sessionExpiry);
// if (sessionCode && sessionExpiry) {
// const sessionExpiryDate = new Date(sessionExpiry);

if (verify2FACode(code, sessionCode, sessionExpiryDate.getTime())) {
extendedSession.twoFactorCode = null;
extendedSession.twoFactorExpiry = null;
} else {
extendedSession.twoFAError = 'Invalid or expired 2FA code.';
}
} else {
extendedSession.twoFAError = '2FA code or expiring time is missing.';
}
// if (verify2FACode(code, sessionCode, sessionExpiryDate.getTime())) {
// extendedSession.twoFactorCode = null;
// extendedSession.twoFactorExpiry = null;
// } else {
// extendedSession.twoFAError = 'Invalid or expired 2FA code.';
// }
// } else {
// extendedSession.twoFAError = '2FA code or expiring time is missing.';
// }

try {
await new Promise<void>((resolve, reject) => {
req.session.save((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
next();
} catch (err) {
return res.status(500).json({ message: 'Error saving session' });
}
};
// try {
// await new Promise<void>((resolve, reject) => {
// req.session.save((err) => {
// if (err) {
// reject(err);
// } else {
// resolve();
// }
// });
// });
// next();
// } catch (err) {
// return res.status(500).json({ message: 'Error saving session' });
// }
// };
18 changes: 9 additions & 9 deletions src/routes/2fa.route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Router } from "express";
import { enable2FA } from "../controllers/2fa.controller";
import { VerifyAccessToken } from "../middleware/verfiyToken";
import { verifyCode } from "./../middleware/2fa.middleware";
import { login } from "../controllers/user.controller";
// import { Router } from "express";
// import { enable2FA } from "../controllers/2fa.controller";
// import { VerifyAccessToken } from "../middleware/verfiyToken";
// import { verifyCode } from "./../middleware/2fa.middleware";
// import { login } from "../controllers/user.controller";

const router = Router();
// const router = Router();

router.post("/enable-2fa", VerifyAccessToken, enable2FA);
router.post("/verify-code", verifyCode, login);
// router.post("/enable-2fa", VerifyAccessToken, enable2FA);
// router.post("/verify-code", verifyCode, login);

export default router;
// export default router;
4 changes: 2 additions & 2 deletions src/routes/user.route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { twoFAController } from "./../middleware/2fa.middleware";


import express,{Request,Response} from "express";
import {
Expand Down Expand Up @@ -30,7 +30,7 @@ route.post("/register", register);
route.patch("/updateuser/:id", editUser);
route.patch("/updatepassword/:id", updatePassword);
route.delete("/deleteuser/:id", VerifyAccessToken, deleteUser);
route.post("/login", twoFAController, login);
route.post("/login", login);
route.post("/addreview/:id", addReview);
route.post("/addfeedback/:id", addFeedback);

Expand Down

0 comments on commit 6d90e72

Please sign in to comment.