1

I have a requirement to test user authentication to see if a user can potentially login to an experience site with their credentials but then to not allow the actual login. To achieve this I used the Site.login() method and retrieve the results. I then want to delete the AuthSession records that were generated to block the user from actually logging in.

The problem Is I just get the following error from my delete method:

System.DmlException: Delete failed. First exception on row 0 with id 0Ak2500003ICRPHCA5; first error: DEPENDENCY_EXISTS, Cannot complete this operation. Failed to delete user session : null : []

My code is below:

@AuraEnabled
  public static Map<String,String> ValidateUserLogin(String Username, String Password){
    Map<String,String> results = new Map<String,String>();
    Try{

      //Find user information
      User currentUser = [SELECT Id, Username from User WHERE Username =:Username LIMIT 1];
      System.Pagereference loginResult = Site.login(
          userName,
          password,
          '/'
        );
        System.debug('Login was successful, the result is: ' + loginResult);
        String loginUrl = loginResult.getUrl();



       

        list<AuthSession> authSeshList = new list<AuthSession>();
        authSeshList = [SELECT Id, CreatedDate, IsCurrent, LoginType, LogoutUrl,UserType, UsersId FROM AuthSession WHERE UsersId =: currentUser.Id AND LoginType = 'Chatter Communities External User' ];

        if(authSeshList.size() > 0){
          delete authSeshList;
        }

        results.put('success','Yes');
        results.put('errorMessage','');
        return results;

    }catch(Exception e){
        results.put('success','No');
        results.put('errorMessage',e.getMessage());
        return results;
    }
  }

The annoying part is when I run the code to retrieve and delete the authSession records in Anonymous Apex it works absolutely fine.

list<AuthSession> authSeshList = new list<AuthSession>();
        authSeshList = [SELECT Id, CreatedDate, IsCurrent, LoginType, LogoutUrl,UserType, UsersId FROM AuthSession WHERE UsersId =: currentUser.Id AND LoginType = 'Chatter Communities External User' ];

        if(authSeshList.size() > 0){
          delete authSeshList;
        }

its only in an apex class that this error pops up. Any suggestions?

5
  • 3
    As per Salesforce's security considerations, a user's AuthSession cannot be deleted by another user who doesn't have 'Manage User' permission. May 31, 2023 at 13:28
  • Thanks Swetha, I tried to get around the issue by using a platform event so that its the Automated Process user that deletes the records. Even that user does not have the permissions to delete the records of another user. Its only the admin. May 31, 2023 at 14:21
  • Ideally the default Automated Process system user should be able to delete sessions for any users. I'm working on repro from my end. Meanwhile, have you tried to run the Platform Event trigger as any other admin user (see developer.salesforce.com/docs/atlas.en-us.platform_events.meta/…). Jun 1, 2023 at 11:08
  • Hi Swetha, thanks again for the response! I got caught up with other work but I am back to fixing up this login issue. I will test out your suggestion and get back to you on if it works :) Hopefully I can get it to run as an admin user which would make life a lot easier! Jun 21, 2023 at 8:13
  • Were you able to test this? Jun 22, 2023 at 5:25

1 Answer 1

1

I have done small POC to check whether we can delete session of another by running trigger as admin(or can be any user with "manage user" permission) and it did worked. Please fallow below step to achieve this.

  1. Create a Platform event.
  2. Create a trigger for that Platform event.
  3. Create a PlatformEventSubscriberConfig and it is responsible from running your platform event trigger as another user which we have to define while creating PlatformEventSubscriberConfig.

By using salesforce tooling API we can create PlatformEventSubscriberConfig

Make a POST Request to this endPoint '/services/data/v58.0/tooling/sobjects/PlatformEventSubscriberConfig'

Request body:

{

"BatchSize": "200",
"DeveloperName":"upToYou",
"MasterLabel":"upToYou",
"PlatformEventConsumerId": "platformEvent Trigger Id",
"UserId": "Id of user"

}

Trigger:

enter image description here

Source: https://developer.salesforce.com/docs/atlas.en-us.244.0.api_tooling.meta/api_tooling/tooling_api_objects_platformeventsubscriberconfig.htm

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .