login flow
This commit is contained in:
@@ -187,7 +187,7 @@ 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
|
||||
appState.profile.email = email
|
||||
hydrateUserState(from: response.result?.customer)
|
||||
routeAfterLogin()
|
||||
}
|
||||
} catch {
|
||||
@@ -221,6 +221,44 @@ struct OtpView: View {
|
||||
root = .main
|
||||
}
|
||||
|
||||
private func hydrateUserState(from customer: CustomerProfile?) {
|
||||
if let customer {
|
||||
appState.profile.id = customer.id
|
||||
appState.profile.name = customer.name
|
||||
appState.profile.email = customer.email
|
||||
appState.profile.phone = customer.phoneNumber ?? ""
|
||||
SessionStateStore.setActiveUserKey(
|
||||
SessionStateStore.makeUserKey(profileId: customer.id, email: customer.email)
|
||||
)
|
||||
|
||||
if let preferred = customer.addressBook?.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
|
||||
}
|
||||
SessionStateStore.saveAddress(appState.address)
|
||||
} else if let cached = SessionStateStore.loadAddress() {
|
||||
appState.address = cached
|
||||
} else {
|
||||
appState.address = AddressState()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
appState.profile.email = email
|
||||
SessionStateStore.setActiveUserKey(
|
||||
SessionStateStore.makeUserKey(profileId: nil, email: email)
|
||||
)
|
||||
if let cached = SessionStateStore.loadAddress() {
|
||||
appState.address = cached
|
||||
} else {
|
||||
appState.address = AddressState()
|
||||
}
|
||||
}
|
||||
|
||||
private func hasConfiguredAddress() -> Bool {
|
||||
if appState.address.selectedId != nil {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user