Skip to content

Commit 1ef952c

Browse files
committed
fix: FCM token 세션 전이 처리
1 parent 356fabd commit 1ef952c

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

Application/App/Sources/App/Handler/FCMTokenSyncHandler.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ final class FCMTokenSyncHandler {
3838
self.store = store
3939
self.notificationCenter = notificationCenter
4040

41+
authService.observeSignedIn()
42+
.removeDuplicates()
43+
.sink { [weak self] isSignedIn in
44+
self?.handleSessionUpdate(isSignedIn: isSignedIn)
45+
}
46+
.store(in: &cancellables)
47+
4148
notificationCenter.publisher(for: .didRefreshFCMToken)
4249
.compactMap { $0.userInfo?["fcmToken"] as? String }
4350
.sink { [weak self] fcmToken in
@@ -61,6 +68,15 @@ final class FCMTokenSyncHandler {
6168
}
6269

6370
private extension FCMTokenSyncHandler {
71+
func handleSessionUpdate(isSignedIn: Bool) {
72+
guard isSignedIn else {
73+
store.setString(nil, forKey: Key.fcmTokenHash)
74+
return
75+
}
76+
77+
requestFCMTokenSync()
78+
}
79+
6480
func requestFCMTokenSync() {
6581
Task { [weak self] in
6682
guard let self else { return }

Application/App/Tests/PushNotification/FCMTokenSyncHandlerTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,57 @@ struct FCMTokenSyncHandlerTests {
173173
_ = handler
174174
}
175175

176+
@Test("로그인 세션 전이 시 현재 FCM token을 저장한다")
177+
func 로그인_세션_전이_시_현재_FCM_token을_저장한다() async throws {
178+
let notificationCenter = NotificationCenter()
179+
let messagingService = PushMessagingServiceSpy(currentFCMToken: "current-token")
180+
let userService = UserServiceSpy()
181+
let authService = AuthServiceSpy(uid: nil)
182+
let store = UserDefaultsStoreSpy()
183+
let handler = FCMTokenSyncHandler(
184+
authService: authService,
185+
messagingService: messagingService,
186+
userService: userService,
187+
store: store,
188+
notificationCenter: notificationCenter
189+
)
190+
191+
authService.updateSession(uid: "user-id")
192+
193+
try await waitUntil {
194+
await userService.updatedFCMTokens == ["current-token"]
195+
}
196+
_ = handler
197+
}
198+
199+
@Test("로그아웃 세션 전이 시 저장된 FCM token hash를 제거한다")
200+
func 로그아웃_세션_전이_시_저장된_FCM_token_hash를_제거한다() async throws {
201+
let notificationCenter = NotificationCenter()
202+
let messagingService = PushMessagingServiceSpy(currentFCMToken: "current-token")
203+
let userService = UserServiceSpy()
204+
let authService = AuthServiceSpy()
205+
let store = UserDefaultsStoreSpy()
206+
let handler = FCMTokenSyncHandler(
207+
authService: authService,
208+
messagingService: messagingService,
209+
userService: userService,
210+
store: store,
211+
notificationCenter: notificationCenter
212+
)
213+
214+
notificationCenter.post(name: .didRequestFCMTokenSync, object: nil)
215+
try await waitUntil {
216+
store.hasStoredString
217+
}
218+
219+
authService.updateSession(uid: nil)
220+
221+
try await waitUntil {
222+
!store.hasStoredString
223+
}
224+
_ = handler
225+
}
226+
176227
}
177228

178229
private actor UserServiceSpy: UserService {

0 commit comments

Comments
 (0)