Quick Reference - Register a login listener:
login and logout events. This can be achieved using the LoginListener class. LoginListener provides the following 4 methods:
| Delegate Method | Information |
|---|---|
| loginSuccess(event) | Informs you that the login was successful and provides you with a user object containing the data for the user that logged in. |
| loginFailure(event) | Informs you about the failure while logging in the user and provides you with the reason for the failure wrapped in an object of the CometChatException class. |
| logoutSuccess() | Informs you about the user being logged out successfully. |
| logoutFailure(event) | Informs you about the failure while logging out the user. The reason for the failure can be obtained from the object of the CometChatException class. |
Add Login Listener
To add theLoginListener, use the addLoginListener() method provided by the SDK. It takes a unique identifier for the listener and an instance of the LoginListener class.
- JavaScript
- TypeScript
Remove Login Listener
To stop receiving events related to login and logout, use theremoveLoginListener() method and pass the ID of the listener that needs to be removed.
- JavaScript
- TypeScript
Best Practices
Best Practices
- Use unique, descriptive listener IDs to avoid conflicts (e.g.,
"login-screen-listener") - Add listeners in component mount lifecycle and remove them on unmount
- Handle all four events to provide a complete user experience
- Use
loginFailureto display user-friendly error messages
Troubleshooting
Troubleshooting
- Listener not firing: Ensure the listener was added before the login/logout event occurs
- Duplicate events: Check that you’re not adding the same listener multiple times without removing it first
- Memory leaks: Verify that
removeLoginListener()is called when the component unmounts