Skip to content

Commit 057aa2e

Browse files
authored
[#377] 최초 가입 직후 ProfileView를 들어가면 얼럿이 뜨는 이슈를 해결한다 (#379)
* fix: 최초 로그인 시 사용자 문서 초기화 완료 후 세션 반영
1 parent 4739755 commit 057aa2e

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

DevLog/Data/Repository/AuthenticationRepositoryImpl.swift

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,29 @@ final class AuthenticationRepositoryImpl: AuthenticationRepository {
2727
}
2828

2929
func signIn(_ provider: AuthProvider) async throws {
30-
let response: AuthDataResponse
31-
switch provider {
32-
case .apple:
33-
response = try await appleAuthService.signIn()
34-
case .github:
35-
response = try await githubAuthService.signIn()
36-
case .google:
37-
response = try await googleAuthService.signIn()
30+
authService.beginSignIn()
31+
32+
do {
33+
let response: AuthDataResponse
34+
switch provider {
35+
case .apple:
36+
response = try await appleAuthService.signIn()
37+
case .github:
38+
response = try await githubAuthService.signIn()
39+
case .google:
40+
response = try await googleAuthService.signIn()
41+
}
42+
43+
try await userService.upsertUser(response)
44+
authService.completeSignIn()
45+
} catch {
46+
if authService.uid != nil {
47+
try? await authService.clearCurrentSession()
48+
}
49+
50+
authService.cancelSignIn()
51+
throw error
3852
}
39-
try await userService.upsertUser(response)
4053
}
4154

4255
func signOut() async throws {

DevLog/Infra/Service/AuthService.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class AuthService {
1616
private let logger = Logger(category: "AuthService")
1717
private let subject = CurrentValueSubject<Bool, Never>(Auth.auth().currentUser != nil)
1818
private var handler: AuthStateDidChangeListenerHandle?
19+
private var isCompletingSignIn = false
1920

2021
var uid: String? {
2122
Auth.auth().currentUser?.uid
@@ -27,9 +28,16 @@ final class AuthService {
2728

2829
init() {
2930
handler = Auth.auth().addStateDidChangeListener { [weak self] _, user in
31+
guard let self else { return }
3032
let signedIn = user != nil
31-
self?.logger.info("Firebase auth state changed. signedIn: \(signedIn)")
32-
self?.subject.send(signedIn)
33+
self.logger.info("Firebase auth state changed. signedIn: \(signedIn)")
34+
35+
if signedIn && self.isCompletingSignIn {
36+
self.logger.info("Delaying signed-in publication until user bootstrap finishes")
37+
return
38+
}
39+
40+
self.subject.send(signedIn)
3341
}
3442
}
3543

@@ -42,6 +50,24 @@ final class AuthService {
4250
subject.eraseToAnyPublisher()
4351
}
4452

53+
func beginSignIn() {
54+
logger.info("Beginning sign-in bootstrap")
55+
isCompletingSignIn = true
56+
subject.send(false)
57+
}
58+
59+
func completeSignIn() {
60+
logger.info("Completing sign-in bootstrap")
61+
isCompletingSignIn = false
62+
subject.send(Auth.auth().currentUser != nil)
63+
}
64+
65+
func cancelSignIn() {
66+
logger.info("Cancelling sign-in bootstrap")
67+
isCompletingSignIn = false
68+
subject.send(Auth.auth().currentUser != nil)
69+
}
70+
4571
func getProviderID() async throws -> String? {
4672
logger.info("Fetching current provider ID")
4773

0 commit comments

Comments
 (0)