Skip to content

Commit

Permalink
✨ (AccountKit): Add email verification state logic
Browse files Browse the repository at this point in the history
  • Loading branch information
macteuts authored and ladislas committed Feb 7, 2024
1 parent fdc457d commit 70a4edb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 12 additions & 3 deletions Modules/AccountKit/Sources/Authentication/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class AuthManager {
} else if let user = authResult?.user {
log.info("User \(user.uid) signed-in successfully. 🎉")
self?.authenticationState.send(.loggedIn)
self?.emailVerificationState.send(user.isEmailVerified)
}
}
}
Expand Down Expand Up @@ -94,16 +95,24 @@ public class AuthManager {
self.authenticationError.eraseToAnyPublisher()
}

var emailVerificationStatePublisher: AnyPublisher<Bool, Never> {
self.emailVerificationState.eraseToAnyPublisher()
}

// MARK: Private

private let authenticationState = CurrentValueSubject<AuthenticationState, Never>(.unknown)
private let authenticationError = PassthroughSubject<Error, Never>()
private let emailVerificationState = PassthroughSubject<Bool, Never>()
private let auth = Auth.auth()
private var cancellables = Set<AnyCancellable>()

private func updateAuthState(for user: User?) {
// TODO(@macteuts): Check email verification Status when relevant
let newState = user != nil ? AuthenticationState.loggedIn : .loggedOut
self.authenticationState.send(newState)
guard let user else {
self.authenticationState.send(.loggedOut)
return
}
self.authenticationState.send(.loggedIn)
self.emailVerificationState.send(user.isEmailVerified)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public class AuthManagerViewModel: ObservableObject {

@Published public var userAuthenticationState: AuthManager.AuthenticationState = .unknown
@Published public var userIsSigningUp = false
@Published public var userEmailIsVerified = false

// MARK: - Alerts

@Published public var errorMessage: String = ""
@Published public var showErrorAlert = false
@Published public var actionRequestMessage: String = ""
@Published public var showactionRequestAlert = false
@Published public var notificationMessage: String = ""
@Published public var showNotificationAlert = false

Expand Down Expand Up @@ -52,6 +55,13 @@ public class AuthManagerViewModel: ObservableObject {
self?.showErrorAlert = true
}
.store(in: &self.cancellables)

self.authManager.emailVerificationStatePublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] state in
self?.userEmailIsVerified = state
}
.store(in: &self.cancellables)
}

private func handleAuthenticationStateChange(state: AuthManager.AuthenticationState) {
Expand All @@ -60,11 +70,11 @@ public class AuthManagerViewModel: ObservableObject {
if self.userIsSigningUp {
self.notificationMessage = String(l10n.AuthManagerViewModel.successfulEmailVerification.characters)
self.showNotificationAlert = true
} else {
// Handle unverified Sign-in
} else if !self.userEmailIsVerified {
self.actionRequestMessage = "Your email hasn't been verified yet. Please verify your email to avoid losing your data."
self.showactionRequestAlert = true
}
case .loggedOut:
log.info("User signed-out successfuly.")
self.resetState()
case .unknown:
break
Expand Down

0 comments on commit 70a4edb

Please sign in to comment.