@@ -696,26 +696,34 @@ export class OrganizationService {
696
696
return response ;
697
697
}
698
698
699
+ /**
700
+ * Method used for generate access token based on client-id and client secret
701
+ * @param clientCredentials
702
+ * @returns session and access token both
703
+ */
699
704
async clientLoginCredentails ( clientCredentials : IClientCredentials ) : Promise < IAccessTokenData > {
700
705
const { clientId, clientSecret } = clientCredentials ;
706
+ // This method used to authenticate the requested user on keycloak
701
707
const authenticationResult = await this . authenticateClientKeycloak ( clientId , clientSecret ) ;
702
708
let addSessionDetails ;
703
- // Fetch organization details for getting the user id
704
- const orgRoleDetails = await this . organizationRepository . getOrgAndAdminUser ( clientId ) ;
705
- this . logger . debug ( `orgRoleDetails::::${ JSON . stringify ( orgRoleDetails ) } ` ) ;
706
- // check seesion details
709
+ // Fetch owner organization details for getting the user id
710
+ const orgRoleDetails = await this . organizationRepository . getOrgAndOwnerUser ( clientId ) ;
711
+ // Fetch the total number of sessions for the requested user to check and restrict the creation of multiple sessions.
707
712
const userSessionDetails = await this . userRepository . fetchUserSessions ( orgRoleDetails [ 'user' ] . id ) ;
708
713
if ( Number ( process . env . SESSIONS_LIMIT ) <= userSessionDetails ?. length ) {
709
714
throw new BadRequestException ( ResponseMessages . user . error . sessionLimitReached ) ;
710
715
}
711
- // Creation sessison and account
716
+ // Session payload
712
717
const sessionData = {
713
718
sessionToken : authenticationResult ?. access_token ,
714
719
userId : orgRoleDetails [ 'user' ] . id ,
715
720
expires : authenticationResult ?. expires_in ,
716
721
sessionType : SessionType . ORG_SESSION
717
722
} ;
718
-
723
+ // Note:
724
+ // Fetch account details to check whether the requested user account exists
725
+ // If the account exists, update it with the latest details and create a new session
726
+ // Otherwise, create a new account and also create the new session
719
727
const fetchAccountDetails = await this . userRepository . checkAccountDetails ( orgRoleDetails [ 'user' ] . id ) ;
720
728
if ( fetchAccountDetails ) {
721
729
const accountData = {
@@ -729,6 +737,9 @@ export class OrganizationService {
729
737
addSessionDetails = await this . userRepository . createSession ( finalSessionData ) ;
730
738
} ) ;
731
739
} else {
740
+ // Note:
741
+ // This else block is mostly used for already registered users on the platform to create their account & session in the database.
742
+ // Once all users are migrated or created their accounts and sessions in the DB, this code can be removed.
732
743
const accountData = {
733
744
sessionToken : authenticationResult ?. access_token ,
734
745
userId : orgRoleDetails [ 'user' ] . id ,
@@ -742,12 +753,11 @@ export class OrganizationService {
742
753
addSessionDetails = await this . userRepository . createSession ( finalSessionData ) ;
743
754
} ) ;
744
755
}
745
- // Response: add session id as cookies
756
+ // Response: add session id
746
757
const finalResponse = {
747
758
...authenticationResult ,
748
759
sessionId : addSessionDetails . id
749
760
} ;
750
- // In fetch session API need to handle the conditon for session is comes from cookies or query parameter
751
761
return finalResponse ;
752
762
}
753
763
0 commit comments