@@ -37,7 +37,7 @@ import { EmailPattern } from '../utils/validation.utils'
3737import { Context } from './../context'
3838import { UserInputError } from 'apollo-server'
3939import { encodeOtpToToken , generateOtp } from '../utils/2WayAuthentication'
40- import jwt from 'jsonwebtoken'
40+ import jwt from 'jsonwebtoken'
4141const octokit = new Octokit ( { auth : `${ process . env . Org_Repo_Access } ` } )
4242
4343const SECRET = ( process . env . SECRET as string ) || 'mysq_unique_secret'
@@ -50,7 +50,7 @@ enum Status {
5050 rejected = 'rejected' ,
5151}
5252
53- export async function logGeoActivity ( user : any , clientIpAdress : string ) {
53+ export async function logGeoActivity ( user : any , clientIpAdress : string ) {
5454 const response = await fetch ( `https://ipapi.co/${ clientIpAdress } /json/` )
5555
5656 const geoData = await response . json ( )
@@ -86,7 +86,7 @@ enum Status {
8686 return geoData
8787}
8888
89- async function loginscount ( organizationName : any , recentLocation : any ) {
89+ export async function loginsCount ( organizationName : any , recentLocation : any ) {
9090 if ( ! organizationName ) return ;
9191
9292
@@ -103,7 +103,7 @@ async function loginscount(organizationName: any, recentLocation: any) {
103103 loginEntry . recentLocation = recentLocation ;
104104 } else {
105105
106- organization . logins . push ( { date : new Date ( ) , loginsCount : 1 , recentLocation} ) ;
106+ organization . logins . push ( { date : new Date ( ) , loginsCount : 1 , recentLocation } ) ;
107107 }
108108
109109
@@ -264,9 +264,9 @@ const resolvers: any = {
264264 } ,
265265 Login : {
266266 user : async ( parent : any ) => {
267-
267+
268268 const user = await User . findById ( parent . user . id )
269-
269+
270270 return user
271271 } ,
272272 } ,
@@ -378,7 +378,7 @@ const resolvers: any = {
378378 if ( ! org ) {
379379 throw new GraphQLError ( 'Organization not found' , { extensions : { code : 'InvalidOrganization' } } ) ;
380380 }
381-
381+
382382 // Find user with populated fields
383383 const user : any = await User . findOne ( { email } ) . populate ( {
384384 path : 'cohort' ,
@@ -391,24 +391,24 @@ const resolvers: any = {
391391 populate : { path : 'organization' , model : Organization , strictPopulate : false }
392392 }
393393 } ) ;
394-
394+
395395 // Check if user exists
396396 if ( ! user ) {
397397 throw new GraphQLError ( 'Invalid credentials' , { extensions : { code : 'AccountNotFound' } } ) ;
398398 }
399-
399+
400400 // Check if account is active
401401 if ( user . status ?. status !== 'active' ) {
402402 throw new GraphQLError ( `Account is ${ user . status ?. status } . Contact admin.` , {
403403 extensions : { code : 'AccountInactive' }
404404 } ) ;
405405 }
406-
406+
407407 // Check if two-factor authentication is enabled
408408 if ( user . twoFactorAuth ) {
409409 const otp = generateOtp ( ) ; // Generate OTP
410410 const TwoWayVerificationToken = encodeOtpToToken ( otp , email ) ; // Encode OTP
411-
411+
412412 // Send email with OTP
413413 await sendEmail (
414414 email ,
@@ -418,7 +418,7 @@ const resolvers: any = {
418418 process . env . ADMIN_EMAIL ,
419419 process . env . ADMIN_PASS
420420 ) ;
421-
421+
422422 // Return response with encoded OTP token and message
423423 return {
424424 message : 'Check your email for the OTP code.' ,
@@ -432,19 +432,27 @@ const resolvers: any = {
432432 if ( ! passwordMatch ) {
433433 throw new GraphQLError ( 'Invalid credentials' , { extensions : { code : 'InvalidCredential' } } ) ;
434434 }
435-
435+
436436 // Generate token for authenticated user
437437 const token = jwt . sign (
438438 { userId : user . _id , role : user . _doc ?. role || 'user' } ,
439439 SECRET ,
440440 { expiresIn : '2h' }
441441 ) ;
442-
442+
443443 const geoData = await logGeoActivity ( user , clientIpAdress ) // Log activity
444-
444+
445+ const organizationName = user . organizations [ 0 ] ;
446+ if ( organizationName ) {
447+ const location = geoData . city && geoData . country_name ? `${ geoData . city } -${ geoData . country_name } ` : null ;
448+ await loginsCount ( organizationName , location ) ;
449+ }
450+
445451 // Return token and user data
446- return { token, user : user . toJSON ( ) ,
447- geoData, otpRequired : false , } ;
452+ return {
453+ token, user : user . toJSON ( ) ,
454+ geoData, otpRequired : false ,
455+ } ;
448456 }
449457 }
450458 ,
0 commit comments