Skip to content

Commit

Permalink
fix: add support for login via username
Browse files Browse the repository at this point in the history
  • Loading branch information
mumer92 committed Oct 31, 2023
1 parent 6fbe09d commit e5512d9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions Authorization/Authorization/SwiftGen/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions Authorization/Authorization/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
18 changes: 11 additions & 7 deletions Core/Core/View/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}

}

0 comments on commit e5512d9

Please sign in to comment.