diff --git a/Authorization/Authorization/Presentation/Login/SignInViewModel.swift b/Authorization/Authorization/Presentation/Login/SignInViewModel.swift index c97689735..2473d127d 100644 --- a/Authorization/Authorization/Presentation/Login/SignInViewModel.swift +++ b/Authorization/Authorization/Presentation/Login/SignInViewModel.swift @@ -50,12 +50,12 @@ public class SignInViewModel: ObservableObject { @MainActor func login(username: String, password: String) async { - guard validator.isValidEmail(username) else { - errorMessage = AuthLocalization.Error.invalidEmailAddress + guard validator.isValidUsernameOrEmail(username) else { + errorMessage = AuthLocalization.Error.invalidEmailAddressOrUsername return } guard validator.isValidPassword(password) else { - errorMessage = AuthLocalization.Error.invalidPasswordLenght + errorMessage = AuthLocalization.Error.invalidPasswordLength return } diff --git a/Authorization/Authorization/SwiftGen/Strings.swift b/Authorization/Authorization/SwiftGen/Strings.swift index 9d7d92b4e..8db3e2bce 100644 --- a/Authorization/Authorization/SwiftGen/Strings.swift +++ b/Authorization/Authorization/SwiftGen/Strings.swift @@ -11,10 +11,12 @@ import Foundation // swiftlint:disable nesting type_body_length type_name vertical_whitespace_opening_braces public enum AuthLocalization { public enum Error { - /// Invalid email address - public static let invalidEmailAddress = AuthLocalization.tr("Localizable", "ERROR.INVALID_EMAIL_ADDRESS", fallback: "Invalid email address") + /// Invalid email + public static let invalidEmailAddress = AuthLocalization.tr("Localizable", "ERROR.INVALID_EMAIL_ADDRESS", fallback: "Invalid email") + /// Invalid email or username + public static let invalidEmailAddressOrUsername = AuthLocalization.tr("Localizable", "ERROR.INVALID_EMAIL_ADDRESS_OR_USERNAME", fallback: "Invalid email or username") /// Invalid password lenght - public static let invalidPasswordLenght = AuthLocalization.tr("Localizable", "ERROR.INVALID_PASSWORD_LENGHT", fallback: "Invalid password lenght") + public static let invalidPasswordLength = AuthLocalization.tr("Localizable", "ERROR.INVALID_PASSWORD_LENGTH", fallback: "Invalid password lenght") } public enum Forgot { /// We have sent a password recover instructions to your email @@ -31,6 +33,8 @@ public enum AuthLocalization { public enum SignIn { /// Email public static let email = AuthLocalization.tr("Localizable", "SIGN_IN.EMAIL", fallback: "Email") + /// Email or username + public static let emailOrUsername = AuthLocalization.tr("Localizable", "SIGN_IN.EMAIL_OR_USERNAME", fallback: "Email or username") /// Forgot password? public static let forgotPassBtn = AuthLocalization.tr("Localizable", "SIGN_IN.FORGOT_PASS_BTN", fallback: "Forgot password?") /// Sign in diff --git a/Authorization/Authorization/en.lproj/Localizable.strings b/Authorization/Authorization/en.lproj/Localizable.strings index 88365042c..beb312f81 100644 --- a/Authorization/Authorization/en.lproj/Localizable.strings +++ b/Authorization/Authorization/en.lproj/Localizable.strings @@ -9,14 +9,15 @@ "SIGN_IN.LOG_IN_TITLE" = "Sign in"; "SIGN_IN.WELCOME_BACK" = "Welcome back! Please authorize to continue."; "SIGN_IN.EMAIL" = "Email"; +"SIGN_IN.EMAIL_OR_USERNAME" = "Email or username"; "SIGN_IN.PASSWORD" = "Password"; "SIGN_IN.REGISTER_BTN" = "Register"; "SIGN_IN.FORGOT_PASS_BTN" = "Forgot password?"; "SIGN_IN.LOG_IN_BTN" = "Sign in"; - -"ERROR.INVALID_EMAIL_ADDRESS" = "Invalid email address"; -"ERROR.INVALID_PASSWORD_LENGHT" = "Invalid password lenght"; +"ERROR.INVALID_EMAIL_ADDRESS" = "Invalid email"; +"ERROR.INVALID_EMAIL_ADDRESS_OR_USERNAME" = "Invalid email or username"; +"ERROR.INVALID_PASSWORD_LENGTH" = "Invalid password lenght"; "SIGN_UP.TITLE" = "Sign up"; "SIGN_UP.SUBTITLE" = "Create new account."; diff --git a/Core/Core/View/Validator.swift b/Core/Core/View/Validator.swift index f44913db2..382a57633 100644 --- a/Core/Core/View/Validator.swift +++ b/Core/Core/View/Validator.swift @@ -9,6 +9,11 @@ import Foundation public class Validator { + private let usernameOrEmailRegex = "^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com|[a-zA-Z0-9._%+-]+)$" + private lazy var usernameOrEmailPredicate = { + NSPredicate(format: "SELF MATCHES %@", usernameOrEmailRegex) + }() + private let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" private lazy var emailPredicate = { NSPredicate(format: "SELF MATCHES %@", emailRegEx) @@ -17,16 +22,15 @@ public class Validator { public init() { } - public func isValidEmail(_ email: String) -> Bool { - return emailPredicate.evaluate(with: email) + public func isValidUsernameOrEmail(_ string: String) -> Bool { + return usernameOrEmailPredicate.evaluate(with: string) } - public func isValidPassword(_ password: String) -> Bool { - return password.count >= 2 + public func isValidEmail(_ string: String) -> Bool { + return emailPredicate.evaluate(with: string) } - public func isValidUsername(_ username: String) -> Bool { - return username.count >= 2 && username.count <= 30 + public func isValidPassword(_ password: String) -> Bool { + return password.count >= 2 } - }