Add auto-login after password reset on iOS and Android

- Add LOGGING_IN step to PasswordResetStep enum on both platforms
- Auto-login with new credentials after successful password reset
- Navigate directly to main app (or verification screen if unverified)
- Show "Logging in..." state during auto-login process
- Hide back button during auto-login to prevent interruption
- Fall back to "Return to Login" if auto-login fails

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-06 00:19:47 -06:00
parent 859a6679ed
commit e13f2702a5
8 changed files with 181 additions and 21 deletions

View File

@@ -17388,6 +17388,9 @@
},
"Log in" : {
},
"Logging in..." : {
},
"Mark Task In Progress" : {
"comment" : "A button label that says \"Mark Task In Progress\".",
@@ -21300,6 +21303,9 @@
"Reset Password" : {
"comment" : "The title of the screen where users can reset their passwords.",
"isCommentAutoGenerated" : true
},
"Resetting..." : {
},
"Residences" : {
"comment" : "A tab label for the \"Residences\" section in the main tab view.",

View File

@@ -336,7 +336,16 @@ struct LoginView: View {
RegisterView()
}
.sheet(isPresented: $showPasswordReset) {
PasswordResetFlow(resetToken: resetToken)
PasswordResetFlow(resetToken: resetToken, onLoginSuccess: { isVerified in
// Update the shared authentication manager
AuthenticationManager.shared.login(verified: isVerified)
if isVerified {
// User is verified, call the success callback
self.onLoginSuccess?()
}
// If not verified, RootView will handle showing VerifyEmailView
})
}
.onChange(of: resetToken) { _, token in
if token != nil {

View File

@@ -3,9 +3,11 @@ import SwiftUI
struct PasswordResetFlow: View {
@StateObject private var viewModel: PasswordResetViewModel
@Environment(\.dismiss) var dismiss
var onLoginSuccess: ((Bool) -> Void)?
init(resetToken: String? = nil) {
init(resetToken: String? = nil, onLoginSuccess: ((Bool) -> Void)? = nil) {
_viewModel = StateObject(wrappedValue: PasswordResetViewModel(resetToken: resetToken))
self.onLoginSuccess = onLoginSuccess
}
var body: some View {
@@ -15,13 +17,22 @@ struct PasswordResetFlow: View {
ForgotPasswordView(viewModel: viewModel)
case .verifyCode:
VerifyResetCodeView(viewModel: viewModel)
case .resetPassword, .success:
case .resetPassword, .loggingIn, .success:
ResetPasswordView(viewModel: viewModel, onSuccess: {
dismiss()
})
}
}
.animation(.easeInOut, value: viewModel.currentStep)
.onAppear {
// Set up callback for auto-login success
viewModel.onLoginSuccess = { [self] isVerified in
// Dismiss the sheet first
dismiss()
// Then call the parent's login success handler
onLoginSuccess?(isVerified)
}
}
}
}

View File

@@ -6,7 +6,8 @@ enum PasswordResetStep: CaseIterable {
case requestCode // Step 1: Enter email
case verifyCode // Step 2: Enter 6-digit code
case resetPassword // Step 3: Set new password
case success // Final: Success confirmation
case loggingIn // Auto-login in progress
case success // Final: Success confirmation (only used if auto-login fails)
}
/// ViewModel for password reset flow.
@@ -24,6 +25,9 @@ class PasswordResetViewModel: ObservableObject {
@Published var currentStep: PasswordResetStep = .requestCode
@Published var resetToken: String?
// Callback for successful login after password reset
var onLoginSuccess: ((Bool) -> Void)?
// MARK: - Initialization
init(resetToken: String? = nil) {
// If we have a reset token from deep link, skip to password reset step
@@ -141,9 +145,10 @@ class PasswordResetViewModel: ObservableObject {
let result = try await APILayer.shared.resetPassword(request: request)
if result is ApiResultSuccess<ResetPasswordResponse> {
self.isLoading = false
self.successMessage = "Password reset successfully! You can now log in with your new password."
self.currentStep = .success
// Password reset successful - now auto-login
self.successMessage = "Password reset successfully! Logging you in..."
self.currentStep = .loggingIn
await self.autoLogin()
} else if let error = result as? ApiResultError {
self.isLoading = false
self.errorMessage = ErrorMessageParser.parse(error.message)
@@ -155,6 +160,51 @@ class PasswordResetViewModel: ObservableObject {
}
}
/// Auto-login after successful password reset
private func autoLogin() async {
// Use the email from the reset flow as the username
let username = email
guard !username.isEmpty else {
// If we don't have the email (e.g., deep link flow), fall back to manual login
self.isLoading = false
self.successMessage = "Password reset successfully! You can now log in with your new password."
self.currentStep = .success
return
}
do {
let loginResult = try await APILayer.shared.login(
request: LoginRequest(username: username, password: newPassword)
)
if let success = loginResult as? ApiResultSuccess<AuthResponse>,
let response = success.data {
let isVerified = response.user.verified
// Initialize lookups
_ = try? await APILayer.shared.initializeLookups()
self.isLoading = false
// Call the login success callback
self.onLoginSuccess?(isVerified)
} else if let error = loginResult as? ApiResultError {
// Auto-login failed, fall back to manual login
print("Auto-login failed: \(error.message)")
self.isLoading = false
self.successMessage = "Password reset successfully! You can now log in with your new password."
self.currentStep = .success
}
} catch {
// Auto-login failed, fall back to manual login
print("Auto-login error: \(error.localizedDescription)")
self.isLoading = false
self.successMessage = "Password reset successfully! You can now log in with your new password."
self.currentStep = .success
}
}
/// Navigate to next step
func moveToNextStep() {
switch currentStep {
@@ -163,6 +213,8 @@ class PasswordResetViewModel: ObservableObject {
case .verifyCode:
currentStep = .resetPassword
case .resetPassword:
currentStep = .loggingIn
case .loggingIn:
currentStep = .success
case .success:
break
@@ -178,7 +230,7 @@ class PasswordResetViewModel: ObservableObject {
currentStep = .requestCode
case .resetPassword:
currentStep = .verifyCode
case .success:
case .loggingIn, .success:
break
}
}

View File

@@ -182,8 +182,11 @@ struct ResetPasswordView: View {
}) {
HStack {
Spacer()
if viewModel.isLoading {
if viewModel.isLoading || viewModel.currentStep == .loggingIn {
ProgressView()
.padding(.trailing, 8)
Text(viewModel.currentStep == .loggingIn ? "Logging in..." : "Resetting...")
.fontWeight(.semibold)
} else {
Label("Reset Password", systemImage: "lock.shield.fill")
.fontWeight(.semibold)
@@ -191,9 +194,9 @@ struct ResetPasswordView: View {
Spacer()
}
}
.disabled(!isFormValid || viewModel.isLoading)
.disabled(!isFormValid || viewModel.isLoading || viewModel.currentStep == .loggingIn)
// Return to Login Button (shown after success)
// Return to Login Button (shown only if auto-login fails)
if viewModel.currentStep == .success {
Button(action: {
viewModel.reset()
@@ -217,8 +220,8 @@ struct ResetPasswordView: View {
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
// Only show back button if not from deep link
if viewModel.resetToken == nil || viewModel.currentStep != .resetPassword {
// Only show back button if not from deep link and not logging in
if (viewModel.resetToken == nil || viewModel.currentStep != .resetPassword) && viewModel.currentStep != .loggingIn {
Button(action: {
if viewModel.currentStep == .success {
viewModel.reset()