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

Add username/password fallback option for login #1214

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Simplenote/AuthViewController+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ extension AuthViewController {

if let title = descriptor.text {
actionView.title = title
} else if let attributedTitle = descriptor.attributedText {
actionView.attributedTitle = attributedTitle
}

actionView.action = descriptor.selector
Expand Down Expand Up @@ -226,6 +228,11 @@ extension AuthViewController {
func pushCodeLoginView() {
pushNewAuthViewController(with: .loginWithCode, state: state)
}

@objc
func pushUsernameAndPasswordView() {
pushNewAuthViewController(with: .loginWithUsernameAndPassword, state: state)
}
}


Expand Down
2 changes: 1 addition & 1 deletion Simplenote/AuthViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<font key="font" metaFont="systemMedium" size="13"/>
</buttonCell>
<constraints>
<constraint firstAttribute="height" constant="20" id="HrL-ss-QSd"/>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="20" id="HrL-ss-QSd"/>
</constraints>
</button>
<customView appearanceType="darkAqua" translatesAutoresizingMaskIntoConstraints="NO" id="WI9-SH-Yrp" userLabel="Actions Separator View">
Expand Down
34 changes: 30 additions & 4 deletions Simplenote/AuthenticationMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ extension AuthenticationMode {
static var loginWithPassword: AuthenticationMode {
buildLoginWithPasswordMode(header: LoginStrings.loginWithEmailEmailHeader)
}


static var loginWithUsernameAndPassword: AuthenticationMode {
buildLoginWithPasswordMode(header: LoginStrings.loginWithEmailEmailHeader, includeUsername: true)
}

/// Auth Mode: Login with Username + Password + Rate Limiting Header
///
static var loginWithPasswordRateLimited: AuthenticationMode {
Expand All @@ -134,10 +138,11 @@ extension AuthenticationMode {

/// 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"),
private static func buildLoginWithPasswordMode(header: String, includeUsername: Bool = false) -> AuthenticationMode {
let inputElements: AuthenticationInputElements = includeUsername ? [.username, .password] : [.password]
return AuthenticationMode(title: NSLocalizedString("Log In with Password", comment: "LogIn Interface Title"),
header: header,
inputElements: [.password],
inputElements: inputElements,
actions: [
AuthenticationActionDescriptor(name: .primary,
selector: #selector(AuthViewController.pressedLogInWithPassword),
Expand All @@ -160,6 +165,10 @@ extension AuthenticationMode {
AuthenticationActionDescriptor(name: .primary,
selector: #selector(AuthViewController.pressedLoginWithMagicLink),
text: MagicLinkStrings.primaryAction),
AuthenticationActionDescriptor(name: .secondary,
selector: #selector(AuthViewController.pushUsernameAndPasswordView),
text: nil,
attributedText: LoginStrings.usernameAndPasswordOption),
AuthenticationActionDescriptor(name: .tertiary,
selector: #selector(AuthViewController.wordpressSSOAction),
text: LoginStrings.wordpressAction)
Expand Down Expand Up @@ -214,6 +223,23 @@ private enum LoginStrings {
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")

/// Returns a formatted Secondary Action String for Optional Username and password login
///
static var usernameAndPasswordOption: NSAttributedString {
let output = NSMutableAttributedString(string: String(), attributes: [
.font: NSFont.preferredFont(forTextStyle: .subheadline)
])

let prefix = NSLocalizedString("We'll email you a code to log in, \nor you can", comment: "Option to login with username and password *PREFIX*: printed in dark color")
let suffix = NSLocalizedString("log in manually.", comment: "Option to login with username and password *SUFFIX*: Concatenated with a space, after the PREFIX, and printed in blue")

output.append(string: prefix, foregroundColor: NSColor(studioColor: ColorStudio.gray60))
output.append(string: " ")
output.append(string: suffix, foregroundColor: NSColor(studioColor: ColorStudio.spBlue60))

return output
}

}

private enum MagicLinkStrings {
Expand Down
9 changes: 7 additions & 2 deletions Simplenote/NSMutableAttributedString+Simplenote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ extension NSMutableAttributedString {

/// Appends the specified String
///
func append(string: String) {
let string = NSAttributedString(string: string)
func append(string: String, foregroundColor: NSColor? = nil) {
var attributes = [NSAttributedString.Key: Any]()
if let foregroundColor = foregroundColor {
attributes[.foregroundColor] = foregroundColor
}

let string = NSAttributedString(string: string, attributes: attributes)
append(string)
}

Expand Down
Loading