11import { AuthenticationError } from 'apollo-server-errors'
22import mongoose from 'mongoose'
33
4- import jwt from 'jsonwebtoken' ;
5- import { generateTokenUserExists } from '../helpers/user.helpers' ;
6- import { sendEmail } from '../utils/sendEmail' ;
7- import { verifyOtpToken } from '../utils/2WayAuthentication' ;
8- import { GraphQLError } from 'graphql' ;
9- import { User } from '../models/user' ;
10- import { logGeoActivity , loginsCount } from './userResolver' ;
4+ import jwt from 'jsonwebtoken'
5+ import { generateTokenUserExists } from '../helpers/user.helpers'
6+ import { sendEmail } from '../utils/sendEmail'
7+ import { verifyOtpToken } from '../utils/2WayAuthentication'
8+ import { GraphQLError } from 'graphql'
9+ import { User } from '../models/user'
10+ import { logGeoActivity , loginsCount } from './userResolver'
1111
1212interface Enable2FAInput {
1313 email : string
@@ -17,7 +17,7 @@ interface Disable2FAInput {
1717 email : string
1818}
1919
20- const SECRET : string = process . env . SECRET ?? 'test_secret '
20+ const SECRET = ( process . env . SECRET as string ) || 'mysq_unique_secret '
2121const resolvers = {
2222 Mutation : {
2323 enableTwoFactorAuth : async ( _ : any , { email } : Enable2FAInput ) => {
@@ -65,9 +65,17 @@ const resolvers = {
6565 // Disable 2FA by clearing the secret and one-time code
6666 user . twoFactorSecret = null
6767 user . twoFactorAuth = false
68- user . oneTimeCode = null
68+ user . TwoWayVerificationToken = null
6969
7070 await user . save ( )
71+ await sendEmail (
72+ email ,
73+ ' Two-Factor Authentication disabled ' ,
74+ 'Two-Factor Authentication has been disabled on your account' ,
75+ null ,
76+ process . env . ADMIN_EMAIL ,
77+ process . env . ADMIN_PASS
78+ )
7179
7280 return 'Two-factor authentication disabled.'
7381 } catch ( error ) {
@@ -77,51 +85,66 @@ const resolvers = {
7785
7886 loginWithTwoFactorAuthentication : async (
7987 _ : any ,
80- { id, email, otp, TwoWayVerificationToken } : { id ?: string ; email ?: string ; otp : string ; TwoWayVerificationToken : string } , context : any
88+ {
89+ id,
90+ email,
91+ otp,
92+
93+ } : {
94+ id ?: string
95+ email ?: string
96+ otp : string
97+
98+ } ,
99+ context : any
81100 ) => {
82- const { clientIpAdress } = context ;
83- // Verify OTP
84- const isValidOtp = verifyOtpToken ( TwoWayVerificationToken , otp ) ;
85-
86- if ( ! isValidOtp ) {
87- throw new GraphQLError ( 'Invalid OTP. Please try again.' ) ;
88- }
101+ const { clientIpAdress } = context
89102
90103 // Fetch user by either ID or email
91- let user : any ;
104+ let user : any
92105 if ( id ) {
93- user = await User . findById ( id ) ;
106+ user = await User . findById ( id )
94107 } else if ( email ) {
95- user = await User . findOne ( { email } ) ;
108+ user = await User . findOne ( { email } )
96109 }
97110
98111 // Check if user was found
99112 if ( ! user ) {
100- throw new GraphQLError ( 'User not found.' ) ;
113+ throw new GraphQLError ( 'User not found.' )
114+ }
115+ // Verify OTP
116+ const isValidOtp = verifyOtpToken ( user . TwoWayVerificationToken , otp )
117+
118+ if ( ! isValidOtp ) {
119+ throw new GraphQLError ( 'Invalid OTP. Please try again.' )
101120 }
102121
103122 // Generate JWT token
104123 const token = jwt . sign (
105124 { userId : user . _id , role : user . _doc ?. role || 'user' } ,
106125 SECRET ,
107126 { expiresIn : '2h' }
108- ) ;
127+ )
109128
110129 const geoData = await logGeoActivity ( user , clientIpAdress )
111- const organizationName = user . organizations [ 0 ] ;
130+ const organizationName = user . organizations [ 0 ]
112131 if ( organizationName ) {
113- const location = geoData . city && geoData . country_name ? `${ geoData . city } -${ geoData . country_name } ` : null ;
114- await loginsCount ( organizationName , location ) ;
132+ const location =
133+ geoData . city && geoData . country_name
134+ ? `${ geoData . city } -${ geoData . country_name } `
135+ : null
136+ await loginsCount ( organizationName , location )
115137 }
138+ user . TwoWayVerificationToken = null
139+ await user . save ( )
116140
117141 return {
118142 token,
119143 user : user . toJSON ( ) ,
120144
121145 message : 'Logged in successfully' ,
122- } ;
146+ }
123147 } ,
124-
125148 } ,
126149}
127150
0 commit comments