From 5587bee1e0eb899e3e9b8a2301918e87e871eeab Mon Sep 17 00:00:00 2001 From: Lucian Cerbu Date: Tue, 17 Dec 2024 10:13:23 +0200 Subject: [PATCH] The following issues were resolved: - Phone number not displayed correctly - Loading the 2FA status in the settings menu --- Permanent/Common/Constants/Constants.swift | 1 + .../Views/TwoStepVerificationView.swift | 2 +- .../Screens/Login/LoginViewModel.swift | 1 + .../ViewModels/SettingsScreenViewModel.swift | 47 +++++++++++++++++++ .../SideMenus/Views/SettingsScreenView.swift | 2 +- 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Permanent/Common/Constants/Constants.swift b/Permanent/Common/Constants/Constants.swift index 5d00caa8..ff3b794e 100644 --- a/Permanent/Common/Constants/Constants.swift +++ b/Permanent/Common/Constants/Constants.swift @@ -167,6 +167,7 @@ extension Constants.Keys.StorageKeys { static let requestPAAccess = "requestPAAccess" static let requestLinkAccess = "requestLinkAccess" static let biometricsAuthEnabled = "biometricsAuthOffEnabledKey" + static let twoFactorAuthEnabled = "twoFactorAuthEnabledKey" static let fcmPushTokenKey = "fcmPushTokenKey" static let modelVersion = "modelVersion" static let minAppVersion = "minAppVersion" diff --git a/Permanent/Modules/AccountSecurity/Views/TwoStepVerificationView.swift b/Permanent/Modules/AccountSecurity/Views/TwoStepVerificationView.swift index 37dd4349..a704d518 100644 --- a/Permanent/Modules/AccountSecurity/Views/TwoStepVerificationView.swift +++ b/Permanent/Modules/AccountSecurity/Views/TwoStepVerificationView.swift @@ -108,7 +108,7 @@ struct TwoStepVerificationView: View { } .frame(maxWidth: .infinity) HStack { - Text("\(method.type.displayName == TwoFactorMethod.MethodType.sms.displayName ? "+1 " : "")\(method.value)") + Text("\(method.value)") .textStyle(UsualSmallXRegularTextStyle()) .foregroundColor(Color(red: 0.35, green: 0.37, blue: 0.5)) diff --git a/Permanent/Modules/Authentication/Screens/Login/LoginViewModel.swift b/Permanent/Modules/Authentication/Screens/Login/LoginViewModel.swift index d1a526a3..782d2376 100644 --- a/Permanent/Modules/Authentication/Screens/Login/LoginViewModel.swift +++ b/Permanent/Modules/Authentication/Screens/Login/LoginViewModel.swift @@ -58,6 +58,7 @@ class LoginViewModel: ObservableObject { AuthenticationManager.shared.login(withUsername: email, password: password) { status in switch status { case .success, .mfaToken, .unknown: + PreferencesManager.shared.removeValue(forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) handler(status) case .error(message: _): diff --git a/Permanent/Modules/SideMenus/ViewModels/SettingsScreenViewModel.swift b/Permanent/Modules/SideMenus/ViewModels/SettingsScreenViewModel.swift index 1b5f3308..cc6ff4f9 100644 --- a/Permanent/Modules/SideMenus/ViewModels/SettingsScreenViewModel.swift +++ b/Permanent/Modules/SideMenus/ViewModels/SettingsScreenViewModel.swift @@ -25,6 +25,10 @@ class SettingsScreenViewModel: ObservableObject { @Published var isLoading: Bool = false @Published var loggedOut: Bool = false + @Published var twoFactorAuthenticationEnabled: Bool? = nil + @Published var isLoading2FAStatus: Bool = false + @Published var twoFactorMethods: [TwoFactorMethod] = [] + init() { getAccountInfo { error in if error != nil { @@ -35,6 +39,11 @@ class SettingsScreenViewModel: ObservableObject { self.getCurrentArchiveThumbnail() } } + if let twoFactorStatus: Bool = PreferencesManager.shared.getValue(forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) { + twoFactorAuthenticationEnabled = twoFactorStatus + } else { + getTwoFAStatus() + } } func getAccountInfo(_ completionBlock: @escaping ((Error?) -> Void) ) { @@ -69,6 +78,43 @@ class SettingsScreenViewModel: ObservableObject { } } + func getTwoFAStatus() { + let operation = APIOperation(AuthenticationEndpoint.getIDPUser) + isLoading2FAStatus = true + operation.execute(in: APIRequestDispatcher()) { [weak self] result in + DispatchQueue.main.async { + self?.isLoading2FAStatus = false + switch result { + case .json(let response, _): + guard let methods: [IDPUserMethodModel] = JSONHelper.convertToModel(from: response) else { + PreferencesManager.shared.set(false, forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) + self?.twoFactorAuthenticationEnabled = false + return + } + + // Convert IDPUserMethodModel to TwoFactorMethod + self?.twoFactorMethods = methods.map { method in + TwoFactorMethod(methodId: method.methodId, + method: method.method, + value: method.value) + } + + // If we have any methods, 2FA is enabled + PreferencesManager.shared.set(!methods.isEmpty, forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) + self?.twoFactorAuthenticationEnabled = !methods.isEmpty + + case .error: + PreferencesManager.shared.set(false, forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) + self?.twoFactorAuthenticationEnabled = false + + default: + PreferencesManager.shared.set(false, forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) + self?.twoFactorAuthenticationEnabled = false + } + } + } + } + func getAccountDetails() { guard let accountData = accountData else { return } @@ -145,6 +191,7 @@ class SettingsScreenViewModel: ObservableObject { if model.isSuccessful == true { handler(.success) EventsManager.resetUser() + PreferencesManager.shared.removeValue(forKey: Constants.Keys.StorageKeys.twoFactorAuthEnabled) } else { handler(.error(message: .errorMessage)) } diff --git a/Permanent/Modules/SideMenus/Views/SettingsScreenView.swift b/Permanent/Modules/SideMenus/Views/SettingsScreenView.swift index 2e7034bc..e79639ad 100644 --- a/Permanent/Modules/SideMenus/Views/SettingsScreenView.swift +++ b/Permanent/Modules/SideMenus/Views/SettingsScreenView.swift @@ -82,7 +82,7 @@ struct SettingsScreenView: View { Button { settingsRouter.navigate(to: .loginAndSecurity, router: settingsRouter) } label: { - CustomSimpleListItemView(image: Image(.securitySettings), titleText: "Login & Security", notificationIcon: true) + CustomSimpleListItemView(image: Image(.securitySettings), titleText: "Login & Security", notificationIcon: !(viewModel.twoFactorAuthenticationEnabled == true)&&(viewModel.isLoading2FAStatus == false)) } Button { settingsRouter.navigate(to: .legacyPlanning, router: settingsRouter)