Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add support for login via username #141

Merged
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 @@ -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 !username.isEmpty else {
mumer92 marked this conversation as resolved.
Show resolved Hide resolved
errorMessage = AuthLocalization.Error.invalidEmailAddressOrUsername
return
}
guard validator.isValidPassword(password) else {
errorMessage = AuthLocalization.Error.invalidPasswordLenght
errorMessage = AuthLocalization.Error.invalidPasswordLength
mumer92 marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
12 changes: 8 additions & 4 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 password lenght
public static let invalidPasswordLenght = AuthLocalization.tr("Localizable", "ERROR.INVALID_PASSWORD_LENGHT", fallback: "Invalid password lenght")
/// 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 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
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";
volodymyr-chekyrta marked this conversation as resolved.
Show resolved Hide resolved
"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";
mumer92 marked this conversation as resolved.
Show resolved Hide resolved
"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 @@ -34,12 +34,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 @@ -59,7 +59,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
11 changes: 3 additions & 8 deletions Core/Core/View/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ import Foundation

public class Validator {

private let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
private let emailRegEx = ".+@.+\\..+"
volodymyr-chekyrta marked this conversation as resolved.
Show resolved Hide resolved
private lazy var emailPredicate = {
NSPredicate(format: "SELF MATCHES %@", emailRegEx)
}()

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
}

}