Skip to content

Commit c9848ed

Browse files
author
Achyut Kumar M
committed
pass error message to failure closure
1 parent 0414e92 commit c9848ed

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

GoInfoGame/GoInfoGame/Login/PasswordAuthenticationViewModel.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class PasswordAuthenticationViewModel: ObservableObject {
1616
username: String,
1717
environment: APIEnvironment,
1818
onSuccess: @escaping () -> Void,
19-
onFailure: @escaping () -> Void
19+
onFailure: @escaping (String) -> Void
2020
) {
2121
isLoading = true
2222
errorMessage = ""
2323

24-
SessionManager.shared.performLogin(username: username, password: password, environment: environment) { success in
24+
SessionManager.shared.performLogin(username: username, password: password, environment: environment) { success, error in
2525
DispatchQueue.main.async {
2626
if success {
2727
BiometricAuthManager.authenticate(reason: "Enable biometric login") { result in
@@ -34,14 +34,14 @@ class PasswordAuthenticationViewModel: ObservableObject {
3434
onSuccess()
3535
case .failure(let msg), .unavailable(let msg):
3636
self.errorMessage = msg
37-
onFailure()
37+
onFailure("An error occured. Please try again.")
3838
}
3939
}
4040
}
4141
} else {
4242
self.isLoading = false
4343
self.errorMessage = "Invalid username or password."
44-
onFailure()
44+
onFailure("Invalid username or password.")
4545
}
4646
}
4747
}

GoInfoGame/GoInfoGame/Login/SessionManager.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ final class SessionManager: ObservableObject {
1717

1818
var lastLoginPassword: String?
1919

20-
func performLogin(username: String, password: String, environment: APIEnvironment, completion: @escaping (Bool) -> Void) {
20+
func performLogin(username: String, password: String, environment: APIEnvironment, completion: @escaping (Bool, String) -> Void) {
2121
let userName = KeychainManager.load(.username, for: environment) ?? username
2222
let postParams = ["username": userName, "password": password]
2323
guard let postBody = try? JSONSerialization.data(withJSONObject: postParams) else {
24-
completion(false)
24+
completion(false, "Failed to serialize JSON")
2525
return
2626
}
2727

@@ -39,13 +39,13 @@ final class SessionManager: ObservableObject {
3939

4040
self.isLoginSuccessful = true
4141
self.hasLoginFailed = false
42-
completion(true)
42+
completion(true, "")
4343

4444
case .failure(let error):
4545
print("Login failed:", error)
4646
self.isLoginSuccessful = false
4747
self.hasLoginFailed = true
48-
completion(false)
48+
completion(false, "Invalid credentials")
4949
}
5050
}
5151
}
@@ -68,11 +68,11 @@ final class SessionManager: ObservableObject {
6868
lastLoginPassword = nil
6969
}
7070

71-
func loginWithBiometrics(for environment: APIEnvironment, completion: @escaping (Bool) -> Void) {
71+
func loginWithBiometrics(for environment: APIEnvironment, completion: @escaping (Bool, String) -> Void) {
7272
guard let user = KeychainManager.load(.username, for: environment),
7373
let pass = KeychainManager.load(.password, for: environment) else {
7474
print(" Missing credentials for biometric login")
75-
completion(false)
75+
completion(false, "Missing credentials for biometric login")
7676
return
7777
}
7878
performLogin(username: user, password: pass, environment: environment, completion: completion)

GoInfoGame/GoInfoGame/Login/ViewModel/PosmLoginViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class PosmLoginViewModel: ObservableObject {
4040

4141
isLoading = true
4242

43-
SessionManager.shared.performLogin(username: username, password: password, environment: environment) { [weak self] success in
43+
SessionManager.shared.performLogin(username: username, password: password, environment: environment) { [weak self] success, error in
4444
guard let self = self else { return }
4545

4646
DispatchQueue.main.async {

0 commit comments

Comments
 (0)