Skip to content

Commit b5ae642

Browse files
committed
Update JWT invalidated listener and event API
* Update the API for the listener, add and removal function names, event name * The listener API is OSUserJwtInvalidatedListener * The event is OSUserJwtInvalidatedEvent
1 parent 8d0d088 commit b5ae642

File tree

10 files changed

+76
-29
lines changed

10 files changed

+76
-29
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#import <UIKit/UIKit.h>
3232
#import <OneSignalFramework/OneSignalFramework.h>
3333

34-
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver, OSNotificationLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener, OSUserStateObserver>
34+
@interface AppDelegate : UIResponder <UIApplicationDelegate, OSNotificationPermissionObserver, OSInAppMessageLifecycleListener, OSPushSubscriptionObserver, OSNotificationLifecycleListener, OSInAppMessageClickListener, OSNotificationClickListener, OSUserStateObserver, OSUserJwtInvalidatedListener>
3535

3636
@property (strong, nonatomic) UIWindow *window;
3737

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
7676
[OneSignal.User addObserver:self];
7777
[OneSignal.Notifications addPermissionObserver:self];
7878
[OneSignal.InAppMessages addClickListener:self];
79-
80-
81-
[OneSignal.User onJwtInvalidatedWithInvalidatedHandler:^(OSJwtInvalidatedEvent * _Nonnull invalidatedEvent) {
82-
NSLog(@"JWT INVALIDATED CALLBACK FOR: %@", invalidatedEvent.externalId);
83-
}];
79+
[OneSignal addUserJwtInvalidatedListener:self];
8480

8581
NSLog(@"UNUserNotificationCenter.delegate: %@", UNUserNotificationCenter.currentNotificationCenter.delegate);
8682

@@ -130,6 +126,10 @@ - (void)onUserStateDidChangeWithState:(OSUserChangedState * _Nonnull)state {
130126
NSLog(@"Dev App onUserStateDidChangeWithState: %@", [state jsonRepresentation]);
131127
}
132128

129+
- (void)onUserJwtInvalidatedWithEvent:(OSUserJwtInvalidatedEvent * _Nonnull)event {
130+
NSLog(@"Dev App onUserJwtInvalidatedWithEvent: %@", [event jsonRepresentation]);
131+
}
132+
133133
#pragma mark OSInAppMessageDelegate
134134

135135
- (void)onClickInAppMessage:(OSInAppMessageClickEvent * _Nonnull)event {

iOS_SDK/OneSignalDevApp/OneSignalDevApp/SwiftTest.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,19 @@
2828
import Foundation
2929
import OneSignalFramework
3030

31-
class SwiftTest: NSObject {
31+
class SwiftTest: NSObject, OSUserJwtInvalidatedListener {
32+
func onUserJwtInvalidated(event: OSUserJwtInvalidatedEvent) {
33+
print("event: \(event.jsonRepresentation())")
34+
print("externalId: \(event.externalId)")
35+
}
36+
3237
func testSwiftUserModel() {
3338
let token1 = OneSignal.User.pushSubscription.token
3439
let token = OneSignal.User.pushSubscription.token
3540
OneSignal.Debug._dump()
41+
OneSignal.login(externalId: "euid", token: "token")
42+
OneSignal.updateUserJwt(externalId: "euid", token: "token")
43+
OneSignal.addUserJwtInvalidatedListener(self)
44+
OneSignal.removeUserJwtInvalidatedListener(self)
3645
}
3746
}

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
DE16C14524D3724700670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
352352
DE16C14724D3727200670EFA /* OneSignalLifecycleObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */; };
353353
DE16C17024D3989A00670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
354-
DE1DD0602C87D87B00787071 /* OSJwtInvalidatedEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE1DD05F2C87D87B00787071 /* OSJwtInvalidatedEvent.swift */; };
354+
DE1DD0602C87D87B00787071 /* OSUserJwtInvalidatedEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE1DD05F2C87D87B00787071 /* OSUserJwtInvalidatedEvent.swift */; };
355355
DE20425E24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
356356
DE20425F24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
357357
DE20426024E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
@@ -1521,7 +1521,7 @@
15211521
CACBAAAB218A662B000ACAA5 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
15221522
DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalLifecycleObserver.m; sourceTree = "<group>"; };
15231523
DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalLifecycleObserver.h; sourceTree = "<group>"; };
1524-
DE1DD05F2C87D87B00787071 /* OSJwtInvalidatedEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSJwtInvalidatedEvent.swift; sourceTree = "<group>"; };
1524+
DE1DD05F2C87D87B00787071 /* OSUserJwtInvalidatedEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSUserJwtInvalidatedEvent.swift; sourceTree = "<group>"; };
15251525
DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIApplication+OneSignal.h"; sourceTree = "<group>"; };
15261526
DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+OneSignal.m"; sourceTree = "<group>"; };
15271527
DE3568E92C88F56600AF447C /* PropertyExecutorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PropertyExecutorTests.swift; sourceTree = "<group>"; };
@@ -2126,7 +2126,7 @@
21262126
3C3130DF2CA383F800906665 /* OSUser.swift */,
21272127
3C3130E22CA3858500906665 /* OSPushSubscription.swift */,
21282128
3C5117162B15C31E00563465 /* OSUserState.swift */,
2129-
DE1DD05F2C87D87B00787071 /* OSJwtInvalidatedEvent.swift */,
2129+
DE1DD05F2C87D87B00787071 /* OSUserJwtInvalidatedEvent.swift */,
21302130
);
21312131
path = Public;
21322132
sourceTree = "<group>";
@@ -4330,7 +4330,7 @@
43304330
files = (
43314331
3CE795F928DB99B500736BD4 /* OSSubscriptionModelStoreListener.swift in Sources */,
43324332
DE69E1AC282ED87A0090BB3D /* OneSignalUserManagerImpl.swift in Sources */,
4333-
DE1DD0602C87D87B00787071 /* OSJwtInvalidatedEvent.swift in Sources */,
4333+
DE1DD0602C87D87B00787071 /* OSUserJwtInvalidatedEvent.swift in Sources */,
43344334
3C5929E32CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift in Sources */,
43354335
3C9AD6CF2B228B7800BC1540 /* OSRequestAddAliases.swift in Sources */,
43364336
3C9AD6D32B228BB000BC1540 /* OSRequestUpdateProperties.swift in Sources */,

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,19 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
7373

7474
var hasCalledStart = false
7575

76-
private var jwtInvalidatedHandler: OSJwtInvalidatedHandler?
7776
let jwtConfig: OSUserJwtConfig
7877

78+
private var _userJwtInvalidatedObserver: OSObservable<OSUserJwtInvalidatedListener, OSUserJwtInvalidatedEvent>?
79+
var userJwtInvalidatedObserver: OSObservable<OSUserJwtInvalidatedListener, OSUserJwtInvalidatedEvent> {
80+
if let observer = _userJwtInvalidatedObserver {
81+
return observer
82+
}
83+
let userJwtInvalidatedObserver = OSObservable<OSUserJwtInvalidatedListener, OSUserJwtInvalidatedEvent>(change: #selector(OSUserJwtInvalidatedListener.onUserJwtInvalidated(event:)))
84+
_userJwtInvalidatedObserver = userJwtInvalidatedObserver
85+
86+
return userJwtInvalidatedObserver
87+
}
88+
7989
var user: OSUserInternal {
8090
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
8191
return _mockUser
@@ -633,6 +643,16 @@ extension OneSignalUserManagerImpl {
633643
// MARK: - JWT
634644

635645
extension OneSignalUserManagerImpl {
646+
@objc
647+
public func addUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
648+
self.userJwtInvalidatedObserver.addObserver(listener)
649+
}
650+
651+
@objc
652+
public func removeUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
653+
self.userJwtInvalidatedObserver.removeObserver(listener)
654+
}
655+
636656
@objc
637657
public func setRequiresUserAuth(_ required: Bool) {
638658
jwtConfig.isRequired = required
@@ -664,12 +684,8 @@ extension OneSignalUserManagerImpl {
664684
}
665685

666686
private func fireJwtExpired(externalId: String) {
667-
guard let jwtInvalidatedHandler = self.jwtInvalidatedHandler else {
668-
return
669-
}
670-
let invalidatedEvent = OSJwtInvalidatedEvent(externalId: externalId)
671-
672-
jwtInvalidatedHandler(invalidatedEvent)
687+
let event = OSUserJwtInvalidatedEvent(externalId: externalId)
688+
userJwtInvalidatedObserver.notifyChange(event)
673689
}
674690

675691
private func getMessageFromJwtError(_ error: NSError) -> String {
@@ -683,10 +699,6 @@ extension OneSignalUserManagerImpl {
683699
}
684700

685701
extension OneSignalUserManagerImpl: OSUser {
686-
public func onJwtInvalidated(invalidatedHandler: @escaping OSJwtInvalidatedHandler) {
687-
jwtInvalidatedHandler = invalidatedHandler
688-
}
689-
690702
public var User: OSUser {
691703
start()
692704
return self

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Public/OSUser.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,4 @@
5757
func removeSms(_ number: String)
5858
// Language
5959
func setLanguage(_ language: String)
60-
// JWT Token Expire
61-
typealias OSJwtInvalidatedHandler = (_ event: OSJwtInvalidatedEvent) -> Void
62-
func onJwtInvalidated(invalidatedHandler: @escaping OSJwtInvalidatedHandler)
6360
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Public/OSJwtInvalidatedEvent.swift renamed to iOS_SDK/OneSignalSDK/OneSignalUser/Source/Public/OSUserJwtInvalidatedEvent.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,20 @@
2525
THE SOFTWARE.
2626
*/
2727

28-
import Foundation
29-
30-
@objc public class OSJwtInvalidatedEvent: NSObject {
28+
@objc public class OSUserJwtInvalidatedEvent: NSObject {
3129
@objc public let externalId: String
3230

3331
init(externalId: String) {
3432
self.externalId = externalId
3533
}
34+
35+
@objc public func jsonRepresentation() -> NSDictionary {
36+
return [
37+
"externalId": externalId
38+
]
39+
}
40+
}
41+
42+
@objc public protocol OSUserJwtInvalidatedListener {
43+
@objc func onUserJwtInvalidated(event: OSUserJwtInvalidatedEvent)
3644
}

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ + (void)logout {
208208
[OneSignalUserManagerImpl.sharedInstance logout];
209209
}
210210

211+
+ (void)addUserJwtInvalidatedListener:(id<OSUserJwtInvalidatedListener>)listener {
212+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"User Jwt Invalidated Listener added successfully"];
213+
[OneSignalUserManagerImpl.sharedInstance addUserJwtInvalidatedListener:listener];
214+
}
215+
216+
+ (void)removeUserJwtInvalidatedListener:(id<OSUserJwtInvalidatedListener>)listener {
217+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"User Jwt Invalidated Listener removed successfully"];
218+
[OneSignalUserManagerImpl.sharedInstance removeUserJwtInvalidatedListener:listener];
219+
}
220+
211221
+ (void)updateUserJwt:(NSString * _Nonnull)externalId withToken:(NSString * _Nonnull)token {
212222
if ([OneSignalConfigManager shouldAwaitAppIdAndLogMissingPrivacyConsentForMethod:@"updateUserJwt"]) {
213223
return;

iOS_SDK/OneSignalSDK/Source/OneSignalFramework.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ typedef void (^OSFailureBlock)(NSError* error);
7070
+ (void)login:(NSString * _Nonnull)externalId;
7171
+ (void)login:(NSString * _Nonnull)externalId withToken:(NSString * _Nullable)token
7272
NS_SWIFT_NAME(login(externalId:token:));
73-
+ (void)updateUserJwt:(NSString * _Nonnull)externalId withToken:(NSString * _Nonnull)token;
73+
+ (void)addUserJwtInvalidatedListener:(id<OSUserJwtInvalidatedListener> _Nonnull)listener NS_REFINED_FOR_SWIFT;
74+
+ (void)removeUserJwtInvalidatedListener:(id<OSUserJwtInvalidatedListener>_Nonnull)listener NS_REFINED_FOR_SWIFT;
75+
+ (void)updateUserJwt:(NSString * _Nonnull)externalId withToken:(NSString * _Nonnull)token
76+
NS_SWIFT_NAME(updateUserJwt(externalId:token:));
7477
+ (void)logout;
7578

7679
#pragma mark Notifications

iOS_SDK/OneSignalSDK/Source/OneSignalSwiftInterface.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import OneSignalNotifications
3333
import OneSignalCore
3434

3535
public extension OneSignal {
36+
static func addUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
37+
__add(listener)
38+
}
39+
40+
static func removeUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
41+
__remove(listener)
42+
}
43+
3644
static var User: OSUser {
3745
return __user()
3846
}

0 commit comments

Comments
 (0)