Skip to content

Commit 1fbdef9

Browse files
nan-licursoragent
andcommitted
feat: [PR8] demo app Identity Verification UI
Exercises the Identity Verification surface end to end: log in with a token, watch a token be rejected and supply a replacement through the invalidated listener, and see how the SDK behaves while the requirement is still unknown. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e5884a7 commit 1fbdef9

8 files changed

Lines changed: 266 additions & 31 deletions

File tree

‎examples/demo/App.xcodeproj/project.pbxproj‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
5B959D44AB09CB821C00AFBF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35F726E64F9B6817F917227C /* ContentView.swift */; };
4242
5F4B7EC8437D1A8D80DF7674 /* NotificationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A20B46F63592FC67B655BEB8 /* NotificationService.swift */; };
4343
638B81D9DA5FD8636BB038B0 /* OneSignalUser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EF49509A218369322ECFA3B9 /* OneSignalUser.framework */; };
44+
673E0146B4B27EB81FCF50DA /* LoginUserDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DD26AED9EA84B0308C1BFE8 /* LoginUserDialog.swift */; };
4445
674995A7A55C13341317E19B /* OSDialog.swift in Sources */ = {isa = PBXBuildFile; fileRef = D261D46C404E325CBA87A9E0 /* OSDialog.swift */; };
4546
68BC99D15FDCB26EB35EBB07 /* OneSignalLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FE9834773C437CC373607693 /* OneSignalLocation.framework */; };
4647
6E3E040FD8A750248E70E46F /* AppModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EA9D80191548D49F09D30B3 /* AppModels.swift */; };
@@ -321,6 +322,7 @@
321322
38138523A8A81A60A77800CA /* NotificationSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSender.swift; sourceTree = "<group>"; };
322323
3A654457BF0A55B54220E669 /* RemoveMultiDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveMultiDialog.swift; sourceTree = "<group>"; };
323324
3B81D7E1A7EB9BB4466C768F /* SendPushSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendPushSection.swift; sourceTree = "<group>"; };
325+
3DD26AED9EA84B0308C1BFE8 /* LoginUserDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginUserDialog.swift; sourceTree = "<group>"; };
324326
3FD8258E807E6672642A32E6 /* UserFetchService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserFetchService.swift; sourceTree = "<group>"; };
325327
4072202943CA64D2CBC38CB5 /* MultiPairInputDialog.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiPairInputDialog.swift; sourceTree = "<group>"; };
326328
4078B5F096680AFA83D1CB85 /* OutcomesSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OutcomesSection.swift; sourceTree = "<group>"; };
@@ -358,7 +360,7 @@
358360
E194A3F19072CB17A8F1A12E /* SmsSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmsSection.swift; sourceTree = "<group>"; };
359361
ECAC7EF0B67920F9FEC4F129 /* TagsSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagsSection.swift; sourceTree = "<group>"; };
360362
F46DFACB9F304B9374F3C570 /* ToastView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastView.swift; sourceTree = "<group>"; };
361-
"TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Build.xcconfig; sourceTree = "<group>"; };
363+
"TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Build.xcconfig; sourceTree = "<group>"; };
362364
/* End PBXFileReference section */
363365

364366
/* Begin PBXFrameworksBuildPhase section */
@@ -504,6 +506,7 @@
504506
2D959B1636916DAEE5FE6278 /* CustomNotificationDialog.swift */,
505507
5984B93007C6B85AFE09045A /* KeyValueRow.swift */,
506508
280B23B41935EAB89C8C6FCB /* ListWidgets.swift */,
509+
3DD26AED9EA84B0308C1BFE8 /* LoginUserDialog.swift */,
507510
4072202943CA64D2CBC38CB5 /* MultiPairInputDialog.swift */,
508511
D261D46C404E325CBA87A9E0 /* OSDialog.swift */,
509512
D54B9DAAEDBE67B73893C522 /* OutcomeDialog.swift */,
@@ -564,10 +567,10 @@
564567
path = Views;
565568
sourceTree = "<group>";
566569
};
567-
"TEMP_9E448277-C21D-48E5-98E2-992293BCF68A" /* demo */ = {
570+
"TEMP_22A9BEEB-F02D-4CD8-B729-B06597DB45B7" /* demo */ = {
568571
isa = PBXGroup;
569572
children = (
570-
"TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */,
573+
"TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */,
571574
);
572575
path = demo;
573576
sourceTree = "<group>";
@@ -810,6 +813,7 @@
810813
7D2BA9022E77B00205453467 /* LiveActivityController.swift in Sources */,
811814
DBDA727D6317A3CCC73A1699 /* LiveActivitySection.swift in Sources */,
812815
B3AE701075398C6A369DBBE0 /* LocationSection.swift in Sources */,
816+
673E0146B4B27EB81FCF50DA /* LoginUserDialog.swift in Sources */,
813817
E3725231A3FD5F5A88BAA758 /* MultiPairInputDialog.swift in Sources */,
814818
4C18E3D284BB28BD846162F3 /* NotificationSender.swift in Sources */,
815819
674995A7A55C13341317E19B /* OSDialog.swift in Sources */,
@@ -936,7 +940,7 @@
936940
/* Begin XCBuildConfiguration section */
937941
0D2EF3911CA89837C30DB0D1 /* Debug */ = {
938942
isa = XCBuildConfiguration;
939-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
943+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
940944
buildSettings = {
941945
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
942946
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
@@ -954,7 +958,7 @@
954958
};
955959
4A0C935808978B5A7673E412 /* Debug */ = {
956960
isa = XCBuildConfiguration;
957-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
961+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
958962
buildSettings = {
959963
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
960964
CODE_SIGN_ENTITLEMENTS = App.entitlements;
@@ -1034,7 +1038,7 @@
10341038
};
10351039
D0E56A85F1C385808720F94B /* Release */ = {
10361040
isa = XCBuildConfiguration;
1037-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
1041+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
10381042
buildSettings = {
10391043
CODE_SIGN_ENTITLEMENTS = OneSignalNotificationServiceExtension/OneSignalNotificationServiceExtension.entitlements;
10401044
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@@ -1056,7 +1060,7 @@
10561060
};
10571061
EB1CC3A930E09FEBECF9195D /* Release */ = {
10581062
isa = XCBuildConfiguration;
1059-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
1063+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
10601064
buildSettings = {
10611065
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
10621066
CODE_SIGN_ENTITLEMENTS = App.entitlements;
@@ -1079,7 +1083,7 @@
10791083
};
10801084
F305A3E63851EE49DA2D190E /* Release */ = {
10811085
isa = XCBuildConfiguration;
1082-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
1086+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
10831087
buildSettings = {
10841088
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
10851089
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
@@ -1102,7 +1106,7 @@
11021106
};
11031107
F5FD25168D9B32A08A468069 /* Debug */ = {
11041108
isa = XCBuildConfiguration;
1105-
baseConfigurationReference = "TEMP_E243B27C-2274-4671-9C94-8B86EB8D4EFA" /* Build.xcconfig */;
1109+
baseConfigurationReference = "TEMP_4292FBC8-5C5B-4FEC-8FC6-E00954AA81D6" /* Build.xcconfig */;
11061110
buildSettings = {
11071111
CODE_SIGN_ENTITLEMENTS = OneSignalNotificationServiceExtension/OneSignalNotificationServiceExtension.entitlements;
11081112
DEVELOPMENT_TEAM = 99SW8E36CT;

‎examples/demo/App/Models/AppModels.swift‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ enum AddItemType {
8080
case tag
8181
case trigger
8282
case externalUserId
83+
case updateUserJwt
8384

8485
var title: String {
8586
switch self {
@@ -89,12 +90,13 @@ enum AddItemType {
8990
case .tag: return "Add Tag"
9091
case .trigger: return "Add Trigger"
9192
case .externalUserId: return "Login User"
93+
case .updateUserJwt: return "Update User JWT"
9294
}
9395
}
9496

9597
var requiresKeyValue: Bool {
9698
switch self {
97-
case .alias, .tag, .trigger: return true
99+
case .alias, .tag, .trigger, .updateUserJwt: return true
98100
case .email, .sms, .externalUserId: return false
99101
}
100102
}
@@ -103,6 +105,7 @@ enum AddItemType {
103105
switch self {
104106
case .alias: return "Label"
105107
case .tag, .trigger: return "Key"
108+
case .updateUserJwt: return "External User Id"
106109
default: return "Key"
107110
}
108111
}
@@ -114,6 +117,7 @@ enum AddItemType {
114117
case .sms: return "Phone Number"
115118
case .tag, .trigger: return "Value"
116119
case .externalUserId: return "External User Id"
120+
case .updateUserJwt: return "JWT Token"
117121
}
118122
}
119123

@@ -141,6 +145,7 @@ enum AddItemType {
141145
case .tag: return "tag"
142146
case .trigger: return "trigger"
143147
case .externalUserId: return "login_user_id"
148+
case .updateUserJwt: return "update_jwt"
144149
}
145150
}
146151

@@ -152,6 +157,7 @@ enum AddItemType {
152157
case .alias: return "alias_label_input"
153158
case .tag: return "tag_key_input"
154159
case .trigger: return "trigger_key_input"
160+
case .updateUserJwt: return "update_jwt_external_id_input"
155161
default: return "\(accessibilityKey)_key_input"
156162
}
157163
}
@@ -165,6 +171,7 @@ enum AddItemType {
165171
case .alias: return "alias_id_input"
166172
case .tag: return "tag_value_input"
167173
case .trigger: return "trigger_value_input"
174+
case .updateUserJwt: return "update_jwt_token_input"
168175
default: return "\(accessibilityKey)_input"
169176
}
170177
}

‎examples/demo/App/Services/OneSignalService.swift‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,39 @@ final class OneSignalService {
9696

9797
// MARK: - User
9898

99-
func login(externalId: String) {
99+
func login(externalId: String, jwtToken: String? = nil) {
100100
prefs.setExternalUserId(externalId)
101-
OneSignal.login(externalId)
101+
// Persist for the demo REST fetch only; cold start does not call login/updateUserJwt with it.
102+
prefs.setSessionJwtToken(jwtToken)
103+
if let jwtToken = jwtToken {
104+
OneSignal.login(externalId: externalId, token: jwtToken)
105+
} else {
106+
OneSignal.login(externalId)
107+
}
108+
}
109+
110+
func updateUserJwt(externalId: String, token: String) {
111+
prefs.setSessionJwtToken(token)
112+
OneSignal.updateUserJwt(externalId: externalId, token: token)
102113
}
103114

104115
func logout() {
105116
prefs.setExternalUserId(nil)
117+
prefs.setSessionJwtToken(nil)
106118
OneSignal.logout()
107119
}
108120

121+
// MARK: - Identity Verification (demo REST fetch)
122+
123+
/// Demo toggle for addressing the REST user fetch by `external_id`. Persisted across launches.
124+
var useIdentityVerification: Bool {
125+
get { prefs.getUseIdentityVerification() }
126+
set { prefs.setUseIdentityVerification(newValue) }
127+
}
128+
129+
/// JWT from the last login / updateUserJwt. Used by the demo REST fetch only — not auto-fed to the SDK on cold start.
130+
var sessionJwtToken: String? { prefs.getSessionJwtToken() }
131+
109132
// MARK: - Aliases
110133

111134
func addAlias(label: String, id: String) { OneSignal.User.addAlias(label: label, id: id) }
@@ -222,6 +245,14 @@ final class OneSignalService {
222245
OneSignal.Notifications.addPermissionObserver(observer)
223246
}
224247

248+
func addUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
249+
OneSignal.addUserJwtInvalidatedListener(listener)
250+
}
251+
252+
func removeUserJwtInvalidatedListener(_ listener: OSUserJwtInvalidatedListener) {
253+
OneSignal.removeUserJwtInvalidatedListener(listener)
254+
}
255+
225256
func addNotificationClickListener(_ listener: OSNotificationClickListener) {
226257
OneSignal.Notifications.addClickListener(listener)
227258
}

‎examples/demo/App/Services/PreferencesService.swift‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import Foundation
2929

3030
/// `UserDefaults`-backed cache for state the demo restores across cold launches:
31-
/// consent flags, IAM paused, location shared, and the last-logged-in external
32-
/// user id. Mirrors the Capacitor demo's `PreferencesService` so the iOS demo
33-
/// re-feeds these into the SDK during initialization.
31+
/// consent flags, IAM paused, location shared, the last-logged-in external user id,
32+
/// the Identity Verification toggle, and the demo REST JWT. The JWT is only for the
33+
/// demo's `/users` fetch — it is not automatically passed to `login` / `updateUserJwt`.
3434
final class PreferencesService {
3535

3636
static let shared = PreferencesService()
@@ -47,6 +47,8 @@ final class PreferencesService {
4747
static let iamPaused = "onesignal.demo.iamPaused"
4848
static let locationShared = "onesignal.demo.locationShared"
4949
static let externalUserId = "onesignal.demo.externalUserId"
50+
static let useIdentityVerification = "onesignal.demo.useIdentityVerification"
51+
static let sessionJwtToken = "onesignal.demo.sessionJwtToken"
5052
}
5153

5254
// MARK: - Consent
@@ -83,4 +85,24 @@ final class PreferencesService {
8385
defaults.removeObject(forKey: Key.externalUserId)
8486
}
8587
}
88+
89+
// MARK: - Identity Verification (demo REST fetch)
90+
91+
func getUseIdentityVerification() -> Bool { defaults.bool(forKey: Key.useIdentityVerification) }
92+
func setUseIdentityVerification(_ value: Bool) { defaults.set(value, forKey: Key.useIdentityVerification) }
93+
94+
func getSessionJwtToken() -> String? {
95+
guard let value = defaults.string(forKey: Key.sessionJwtToken), !value.isEmpty else {
96+
return nil
97+
}
98+
return value
99+
}
100+
101+
func setSessionJwtToken(_ value: String?) {
102+
if let value = value, !value.isEmpty {
103+
defaults.set(value, forKey: Key.sessionJwtToken)
104+
} else {
105+
defaults.removeObject(forKey: Key.sessionJwtToken)
106+
}
107+
}
86108
}

‎examples/demo/App/Services/UserFetchService.swift‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,36 @@
2727

2828
import Foundation
2929

30-
/// Reads the OneSignal /users API to hydrate aliases / tags / channels in the demo
30+
/// Reads the OneSignal /users API to hydrate aliases / tags / channels in the demo.
3131
final class UserFetchService {
3232
static let shared = UserFetchService()
3333
private init() {}
3434

35-
func fetchUser(appId: String, onesignalId: String) async -> UserData? {
36-
let urlString = "https://api.onesignal.com/apps/\(appId)/users/by/onesignal_id/\(onesignalId)"
37-
guard let url = URL(string: urlString) else { return nil }
35+
/// Fetches by `onesignal_id` (unsigned) or `external_id` (Bearer when `jwt` is set).
36+
func fetchUser(
37+
appId: String,
38+
aliasLabel: String,
39+
aliasValue: String,
40+
jwt: String? = nil
41+
) async -> UserData? {
42+
guard !aliasValue.isEmpty else { return nil }
43+
44+
// Path-encode so external_ids with reserved characters don't misroute the GET.
45+
var allowed = CharacterSet.urlPathAllowed
46+
allowed.remove(charactersIn: "/")
47+
guard
48+
let encodedAlias = aliasValue.addingPercentEncoding(withAllowedCharacters: allowed),
49+
let url = URL(string: "https://api.onesignal.com/apps/\(appId)/users/by/\(aliasLabel)/\(encodedAlias)")
50+
else {
51+
return nil
52+
}
3853

3954
var request = URLRequest(url: url)
4055
request.httpMethod = "GET"
56+
request.setValue("application/json", forHTTPHeaderField: "Accept")
57+
if let jwt = jwt {
58+
request.setValue("Bearer \(jwt)", forHTTPHeaderField: "Authorization")
59+
}
4160

4261
do {
4362
let (data, response) = try await URLSession.shared.data(for: request)

0 commit comments

Comments
 (0)