Skip to content

Commit 8a66205

Browse files
authored
Merge pull request #781 from Iterable/jwt/SDK-149-add-logout-functionality
[SDK-149] add-logout-functionality
2 parents 1d74fed + b9401df commit 8a66205

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

src/core/classes/Iterable.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,58 @@ describe('Iterable', () => {
7474
});
7575
});
7676

77+
describe('logout', () => {
78+
it('should call setEmail with null', () => {
79+
// GIVEN no parameters
80+
// WHEN Iterable.logout is called
81+
const setEmailSpy = jest.spyOn(Iterable, 'setEmail');
82+
Iterable.logout();
83+
// THEN Iterable.setEmail is called with null
84+
expect(setEmailSpy).toBeCalledWith(null);
85+
setEmailSpy.mockRestore();
86+
});
87+
88+
it('should call setUserId with null', () => {
89+
// GIVEN no parameters
90+
// WHEN Iterable.logout is called
91+
const setUserIdSpy = jest.spyOn(Iterable, 'setUserId');
92+
Iterable.logout();
93+
// THEN Iterable.setUserId is called with null
94+
expect(setUserIdSpy).toBeCalledWith(null);
95+
setUserIdSpy.mockRestore();
96+
});
97+
98+
it('should clear email and userId', async () => {
99+
// GIVEN a user is logged in
100+
101+
// This is just for testing purposed.
102+
// Usually you'd either call `setEmail` or `setUserId`, but not both.
103+
Iterable.setEmail('user@example.com');
104+
Iterable.setUserId('user123');
105+
// WHEN Iterable.logout is called
106+
Iterable.logout();
107+
// THEN email and userId are set to null
108+
const email = await Iterable.getEmail();
109+
const userId = await Iterable.getUserId();
110+
expect(email).toBeNull();
111+
expect(userId).toBeNull();
112+
});
113+
114+
it('should call setEmail and setUserId with null', () => {
115+
// GIVEN no parameters
116+
const setEmailSpy = jest.spyOn(Iterable, 'setEmail');
117+
const setUserIdSpy = jest.spyOn(Iterable, 'setUserId');
118+
// WHEN Iterable.logout is called
119+
Iterable.logout();
120+
// THEN both methods are called with null
121+
expect(setEmailSpy).toBeCalledWith(null);
122+
expect(setUserIdSpy).toBeCalledWith(null);
123+
// Clean up
124+
setEmailSpy.mockRestore();
125+
setUserIdSpy.mockRestore();
126+
});
127+
});
128+
77129
describe('disableDeviceForCurrentUser', () => {
78130
it('should disable the device for the current user', () => {
79131
// GIVEN no parameters

src/core/classes/Iterable.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,34 @@ export class Iterable {
890890
});
891891
}
892892

893+
/**
894+
* Logs out the current user from the Iterable SDK.
895+
*
896+
* This method will remove all event listeners for the Iterable SDK and set the email and user ID to null.
897+
*
898+
* @example
899+
* ```typescript
900+
* Iterable.logout();
901+
* ```
902+
*/
903+
static logout() {
904+
Iterable.removeAllEventListeners();
905+
Iterable.setEmail(null);
906+
Iterable.setUserId(null);
907+
}
908+
909+
/**
910+
* Removes all event listeners for the Iterable SDK.
911+
*/
912+
private static removeAllEventListeners() {
913+
RNEventEmitter.removeAllListeners(IterableEventName.handleUrlCalled);
914+
RNEventEmitter.removeAllListeners(IterableEventName.handleInAppCalled);
915+
RNEventEmitter.removeAllListeners(IterableEventName.handleCustomActionCalled);
916+
RNEventEmitter.removeAllListeners(IterableEventName.handleAuthCalled);
917+
RNEventEmitter.removeAllListeners(IterableEventName.handleAuthSuccessCalled);
918+
RNEventEmitter.removeAllListeners(IterableEventName.handleAuthFailureCalled);
919+
}
920+
893921
/**
894922
* Sets up event handlers for various Iterable events.
895923
*
@@ -912,12 +940,7 @@ export class Iterable {
912940
*/
913941
private static setupEventHandlers() {
914942
// Remove all listeners to avoid duplicate listeners
915-
RNEventEmitter.removeAllListeners(IterableEventName.handleUrlCalled);
916-
RNEventEmitter.removeAllListeners(IterableEventName.handleInAppCalled);
917-
RNEventEmitter.removeAllListeners(
918-
IterableEventName.handleCustomActionCalled
919-
);
920-
RNEventEmitter.removeAllListeners(IterableEventName.handleAuthCalled);
943+
Iterable.removeAllEventListeners();
921944

922945
if (Iterable.savedConfig.urlHandler) {
923946
RNEventEmitter.addListener(IterableEventName.handleUrlCalled, (dict) => {

0 commit comments

Comments
 (0)