Skip to content

Commit

Permalink
AuthenticationMode: RateLimiting Fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jleandroperez committed Jul 26, 2024
1 parent 7e5a60a commit d2481f6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Simplenote/AuthViewController+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ extension AuthViewController {
showLoginCodeExpiredAlert()

case .tooManyAttempts:
if let fallbackMode = mode.rateLimitingFallbackMode?() {
pushNewAuthViewController(with: fallbackMode, state: state)
break
}

/// No fallback =(
let message = NSLocalizedString("Too many log in attempts. Try again later.", comment: "Error for too many login attempts")
showAuthenticationError(message)

Expand Down
29 changes: 24 additions & 5 deletions Simplenote/AuthenticationMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,23 @@ class AuthenticationMode: NSObject {
let header: String?
let inputElements: AuthenticationInputElements
let actions: [AuthenticationActionDescriptor]

let primaryActionAnimationText: String

let rateLimitingFallbackMode: (() -> AuthenticationMode)?
let isIntroView: Bool

init(title: String,
header: String? = nil,
inputElements: AuthenticationInputElements,
actions: [AuthenticationActionDescriptor],
primaryActionAnimationText: String,
rateLimitingFallbackMode: (() -> AuthenticationMode)? = nil,
isIntroView: Bool = false) {
self.title = title
self.header = header
self.inputElements = inputElements
self.actions = actions
self.primaryActionAnimationText = primaryActionAnimationText
self.rateLimitingFallbackMode = rateLimitingFallbackMode
self.isIntroView = isIntroView
}
}
Expand All @@ -78,6 +79,7 @@ extension AuthenticationMode {
}
}


// MARK: - Public Properties
//
extension AuthenticationMode {
Expand Down Expand Up @@ -121,8 +123,20 @@ extension AuthenticationMode {
/// Auth Mode: Login with Username + Password
///
static var loginWithPassword: AuthenticationMode {
buildLoginWithPasswordMode(header: LoginStrings.loginWithEmailEmailHeader)
}

/// Auth Mode: Login with Username + Password + Rate Limiting Header
///
static var loginWithPasswordRateLimited: AuthenticationMode {
buildLoginWithPasswordMode(header: LoginStrings.loginWithEmailLimitHeader)
}

/// Builds the loginWithPassword Mode with the specified Header
///
private static func buildLoginWithPasswordMode(header: String) -> AuthenticationMode {
AuthenticationMode(title: NSLocalizedString("Log In with Password", comment: "LogIn Interface Title"),
header: LoginStrings.loginWithEmailEmailHeader,
header: header,
inputElements: [.password],
actions: [
AuthenticationActionDescriptor(name: .primary,
Expand All @@ -143,14 +157,17 @@ extension AuthenticationMode {
AuthenticationMode(title: NSLocalizedString("Log In", comment: "LogIn Interface Title"),
inputElements: [.username, .actionSeparator],
actions: [
AuthenticationActionDescriptor(name: .primary,
AuthenticationActionDescriptor(name: .primary,
selector: #selector(AuthViewController.pressedLoginWithMagicLink),
text: MagicLinkStrings.primaryAction),
AuthenticationActionDescriptor(name: .tertiary,
selector: #selector(AuthViewController.wordpressSSOAction),
text: LoginStrings.wordpressAction)
],
primaryActionAnimationText: MagicLinkStrings.primaryAnimationText)
primaryActionAnimationText: MagicLinkStrings.primaryAnimationText,
rateLimitingFallbackMode: {
AuthenticationMode.loginWithPasswordRateLimited
})
}

/// Auth Mode: SignUp
Expand Down Expand Up @@ -197,6 +214,8 @@ private enum LoginStrings {
static let switchTip = NSLocalizedString("Need an account?", comment: "Link to create an account")
static let wordpressAction = NSLocalizedString("Log in with WordPress.com", comment: "Title to use wordpress login instead of email")
static let loginWithEmailEmailHeader = NSLocalizedString("Enter the password for the account {{EMAIL}}", comment: "Header for Login With Password. Please preserve the {{EMAIL}} substring")
static let loginWithEmailLimitHeader = NSLocalizedString("Log in with email failed, please enter the password for {{EMAIL}}", comment: "Header for Enter Password UI, when the user performed too many requests")

}

private enum MagicLinkStrings {
Expand Down

0 comments on commit d2481f6

Please sign in to comment.