-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
79 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
// } | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters