1
- import { Request , Response , NextFunction } from " express" ;
2
- import { Session } from " express-session" ;
3
- import { generate2FACode , verify2FACode } from " ../services/2fa.service" ;
1
+ import { Request , Response , NextFunction } from ' express' ;
2
+ import { Session } from ' express-session' ;
3
+ import { generate2FACode , verify2FACode } from ' ../services/2fa.service' ;
4
4
5
- // Extend the Session interface
6
5
interface ExtendedSession extends Session {
7
6
email ?: string ;
8
7
password ?: string ;
@@ -11,7 +10,6 @@ interface ExtendedSession extends Session {
11
10
twoFAError ?: string ;
12
11
}
13
12
14
- // Extend the Request interface
15
13
interface ExtendedRequest extends Request {
16
14
session : ExtendedSession ;
17
15
}
@@ -27,12 +25,12 @@ export const twoFAController = async (
27
25
28
26
if ( twoFactorData ) {
29
27
extSession . twoFactorCode = twoFactorData . twoFactorCode ;
30
- if ( typeof twoFactorData . twoFactorExpiry === " number" ) {
28
+ if ( typeof twoFactorData . twoFactorExpiry === ' number' ) {
31
29
extSession . twoFactorExpiry = new Date ( twoFactorData . twoFactorExpiry ) ;
32
30
}
33
31
extSession . email = email ;
34
32
extSession . password = password ;
35
- return res . status ( 200 ) . json ( { message : " 2FA code sent. Please verify the code." } ) ;
33
+ return res . status ( 200 ) . json ( { message : ' 2FA code sent. Please verify the code.' } ) ;
36
34
} else {
37
35
next ( ) ;
38
36
}
@@ -56,24 +54,24 @@ export const verifyCode = async (
56
54
extendedSession . twoFactorCode = null ;
57
55
extendedSession . twoFactorExpiry = null ;
58
56
} else {
59
- extendedSession . twoFAError = " Invalid or expired 2FA code." ;
57
+ extendedSession . twoFAError = ' Invalid or expired 2FA code.' ;
60
58
}
61
59
} else {
62
- extendedSession . twoFAError = " 2FA code or expiring time is missing." ;
60
+ extendedSession . twoFAError = ' 2FA code or expiring time is missing.' ;
63
61
}
64
62
65
63
try {
66
- await new Promise ( ( resolve , reject ) => {
64
+ await new Promise < void > ( ( resolve , reject ) => {
67
65
req . session . save ( ( err ) => {
68
66
if ( err ) {
69
67
reject ( err ) ;
70
68
} else {
71
- resolve ( null ) ;
69
+ resolve ( ) ;
72
70
}
73
71
} ) ;
74
72
} ) ;
75
73
next ( ) ;
76
74
} catch ( err ) {
77
- return res . status ( 500 ) . json ( { message : " Error saving session" } ) ;
75
+ return res . status ( 500 ) . json ( { message : ' Error saving session' } ) ;
78
76
}
79
77
} ;
0 commit comments