Search code examples
apostrophe-cms

How to edit the url to log out of ApostropheCMS


I'm working on a particular strategy to login OAuth2 to ApostropheCMS. After logging into the system, I need to close the session through the url of the identity server that allows me to do the OAuth2 flow.

Instead of using http://localhost:3000/logout, use http://localhost:port/auth/oauth2/logout. But I can't find where to edit the url using the log out link in ApostropheCMS.


Solution

  • I believe you are saying that you don't need to duplicate the logout functionality (you already did that), you need to change the logout link in the admin bar. Fortunately there is a good way to do that.

    Like most modules, the apostrophe-login module adds its admin bar buttons in a method called addAdminBarItems. That method looks like this:

        self.addAdminBarItems = function() {
          var items = [];
          var key;
          if (self.options.resetKnownPassword) {
            key = self.__meta.name + '-reset-known-password';
            self.apos.adminBar.add(key, 'Change Password', null);
            items.push(key);
          }
          key = self.__meta.name + '-logout';
          self.apos.adminBar.add(key, 'Log Out', null, { last: true, href: '/logout' });
          items.push(key);
          if (items.length > 1) {
            self.apos.adminBar.group({
              label: 'Account',
              items: items,
              last: true
            });
          }
        };
    

    We can override it to suit our needs.

    Create lib/modules/apostrophe-login/index.js at project level. Do not copy and paste the whole thing from node_modules, this is never necessary, we are overriding one method here. Apostrophe will automatically see that and apply our changes to the original.

    Here's what the file will look like:

    // in lib/modules/apostrophe-login/index.js of your project
    module.exports = {
      construct: function(self, options) {
        self.addAdminBarItems = function() {
          key = self.__meta.name + '-logout';
          self.apos.adminBar.add(key, 'Log Out', null, { last: true, href: '/anywhere/you/want/it/to/go' });
        };
      }
    };
    

    This will replace the method we need to change, without altering anything else.