Search code examples
javascriptauth0auth0-lock

How to remove a listener attached to auth0-lock?


Auth0-lock documentation provides an example of attaching a listener to the authentication status change event:

https://auth0.com/docs/libraries/lock/v11#2-authenticating-and-getting-user-info

// Listening for the authenticated event
lock.on("authenticated", function(authResult) {
  // Use the token in authResult to getUserInfo() and save it to localStorage
  lock.getUserInfo(authResult.accessToken, function(error, profile) {
    if (error) {
      // Handle error
      return;
    }

    document.getElementById('nick').textContent = profile.nickname;

    localStorage.setItem('accessToken', authResult.accessToken);
    localStorage.setItem('profile', JSON.stringify(profile));
  });
});

API Reference for Auth0 Lock v11 provides more details about event types supported by on, however nothing on the subject of removing a listener:

https://auth0.com/docs/libraries/lock/v11/api#on-

How do I remove a listener set up as per the example above?


Solution

  • I had the same problem and don't know why there is no documentation on how to remove the listener if you need to destroy the lock widget or reset it for some reason.

    I think the following works:

    lock.removeAllListeners('authenticated')
    

    If you want to remove all listeners, just omit 'authenticated' as a parameter