Fix login

This commit is contained in:
Daniel Arantes Loverde
2026-02-21 18:51:39 -03:00
parent 0f5ad4c268
commit 3a3dc7217b
17 changed files with 1296 additions and 91 deletions

View File

@@ -187,8 +187,8 @@ struct OtpView: View {
SnackbarCenter.shared.show(title: "Login realizado com sucesso.", style: .success, icon: "checkmark.seal.fill", duration: 2.5)
appState.session.isAuthenticated = true
appState.session.jwt = response.result?.token
hydrateUserState(from: response.result?.customer)
routeAfterLogin()
let hasServerAddress = hydrateUserState(from: response.result?.customer)
routeAfterLogin(hasServerAddress: hasServerAddress)
}
} catch {
await MainActor.run {
@@ -201,16 +201,8 @@ struct OtpView: View {
}
}
private func routeAfterLogin() {
if let cached = LocationService.shared.cachedLocation() {
appState.address.latitude = cached.0
appState.address.longitude = cached.1
selectedTab = .home
root = .main
return
}
if hasConfiguredAddress() {
private func routeAfterLogin(hasServerAddress: Bool) {
if hasServerAddress || hasConfiguredAddress() {
selectedTab = .home
root = .main
return
@@ -221,7 +213,7 @@ struct OtpView: View {
root = .main
}
private func hydrateUserState(from customer: CustomerProfile?) {
private func hydrateUserState(from customer: CustomerProfile?) -> Bool {
if let customer {
appState.profile.id = customer.id
appState.profile.name = customer.name
@@ -236,21 +228,24 @@ struct OtpView: View {
appState.cart = CartState()
}
if let preferred = customer.addressBook?.first {
let addresses = customer.addressBook ?? []
if let preferred = addresses.first {
appState.address.selectedId = preferred.id
let label = (preferred.label ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
appState.address.display = label.isEmpty ? "Defina seu endereco" : label
if let lat = preferred.latLong?.first, let lng = preferred.latLong?.dropFirst().first {
appState.address.latitude = lat
appState.address.longitude = lng
} else {
appState.address.latitude = nil
appState.address.longitude = nil
}
SessionStateStore.saveAddress(appState.address)
} else if let cached = SessionStateStore.loadAddress() {
appState.address = cached
} else {
appState.address = AddressState()
SessionStateStore.clearAddress()
}
return
return addresses.isEmpty == false
}
appState.profile.email = email
@@ -262,11 +257,8 @@ struct OtpView: View {
} else {
appState.cart = CartState()
}
if let cached = SessionStateStore.loadAddress() {
appState.address = cached
} else {
appState.address = AddressState()
}
appState.address = AddressState()
return false
}
private func hasConfiguredAddress() -> Bool {
@@ -274,10 +266,6 @@ struct OtpView: View {
return true
}
if appState.address.latitude != nil, appState.address.longitude != nil {
return true
}
let normalized = appState.address.display
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()