Skip to content

Commit

Permalink
fix: add support for login via username (#141)
Browse files Browse the repository at this point in the history
* fix: add support for login via username

* chore: fix typo in test case

* chore: add check to validate if username is not empty

* chore: address feedback

* chore: add check for white space on email

* chore: handle white spaces in username validation

* chore: address feedback

* fix: remove unused translation

* Revert "fix: remove unused translation"

This reverts commit 4c5120c.
  • Loading branch information
mumer92 authored Nov 27, 2023
1 parent 7fc5a64 commit 375f99b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public struct SignInView: View {
.foregroundColor(Theme.Colors.textPrimary)
.padding(.bottom, 20)

Text(AuthLocalization.SignIn.email)
Text(AuthLocalization.SignIn.emailOrUsername)
.font(Theme.Fonts.labelLarge)
.foregroundColor(Theme.Colors.textPrimary)
TextField(AuthLocalization.SignIn.email, text: $email)
TextField(AuthLocalization.SignIn.emailOrUsername, text: $email)
.keyboardType(.emailAddress)
.textContentType(.emailAddress)
.autocapitalization(.none)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public class SignInViewModel: ObservableObject {

@MainActor
func login(username: String, password: String) async {
guard validator.isValidEmail(username) else {
errorMessage = AuthLocalization.Error.invalidEmailAddress
guard validator.isValidUsername(username) else {
errorMessage = AuthLocalization.Error.invalidEmailAddressOrUsername
return
}
guard validator.isValidPassword(password) else {
errorMessage = AuthLocalization.Error.invalidPasswordLenght
guard !password.isEmpty else {
errorMessage = AuthLocalization.Error.invalidPasswordLength
return
}

Expand Down
8 changes: 6 additions & 2 deletions Authorization/Authorization/SwiftGen/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ 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 password lenght
public static let invalidPasswordLenght = AuthLocalization.tr("Localizable", "ERROR.INVALID_PASSWORD_LENGHT", fallback: "Invalid password lenght")
/// Invalid email or username
public static let invalidEmailAddressOrUsername = AuthLocalization.tr("Localizable", "ERROR.INVALID_EMAIL_ADDRESS_OR_USERNAME", fallback: "Invalid email or username")
/// Invalid password length
public static let invalidPasswordLength = AuthLocalization.tr("Localizable", "ERROR.INVALID_PASSWORD_LENGTH", fallback: "Invalid password length")
}
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
5 changes: 3 additions & 2 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_OR_USERNAME" = "Invalid email or username";
"ERROR.INVALID_PASSWORD_LENGTH" = "Invalid password length";

"SIGN_UP.TITLE" = "Sign up";
"SIGN_UP.SUBTITLE" = "Create new account.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ final class SignInViewModelTests: XCTestCase {
validator: validator
)

await viewModel.login(username: "email", password: "")
await viewModel.login(username: "", password: "")

Verify(interactor, 0, .login(username: .any, password: .any))
Verify(router, 0, .showMainOrWhatsNewScreen())

XCTAssertEqual(viewModel.errorMessage, AuthLocalization.Error.invalidEmailAddress)
XCTAssertEqual(viewModel.errorMessage, AuthLocalization.Error.invalidEmailAddressOrUsername)
XCTAssertEqual(viewModel.isShowProgress, false)
}

Expand All @@ -61,7 +61,7 @@ final class SignInViewModelTests: XCTestCase {
Verify(interactor, 0, .login(username: .any, password: .any))
Verify(router, 0, .showMainOrWhatsNewScreen())

XCTAssertEqual(viewModel.errorMessage, AuthLocalization.Error.invalidPasswordLenght)
XCTAssertEqual(viewModel.errorMessage, AuthLocalization.Error.invalidPasswordLength)
XCTAssertEqual(viewModel.isShowProgress, false)
}

Expand Down
14 changes: 9 additions & 5 deletions Core/Core/View/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ public class Validator {
public init() {
}

public func isValidEmail(_ email: String) -> Bool {
return emailPredicate.evaluate(with: email)
public func isValidEmail(_ string: String) -> Bool {
return emailPredicate.evaluate(with: string)
}

public func isValidPassword(_ password: String) -> Bool {
return password.count >= 2
}

public func isValidUsername(_ username: String) -> Bool {
return username.count >= 2 && username.count <= 30
public func isValidUsername(_ string: String) -> Bool {
let trimmedString = string.trimmingCharacters(in: .whitespaces)
if trimmedString.contains("@") {
return emailPredicate.evaluate(with: trimmedString)
} else {
return !trimmedString.isEmpty
}
}

}

0 comments on commit 375f99b

Please sign in to comment.