Skip to content

Commit 75457dd

Browse files
committed
Show stored username in General preference pane
This reverts the change from 90c0679 so that the username is shown in situations where we don't have a valid session but could almost certainly get one. Instead, to achieve what that commit was trying to do, we'll instead remove the username from UserDefaults if auth fails with an invalid username or password error. I think this will more closely match what users expect in this UI. I've added a comment in the UI explaining why it is the way it is. It might also be worth considering renaming AuthenticationState or its cases to better reflect that it's probably more about the (short-lived) session state than whether the user has signed in before and has stored their credentials.
1 parent 35dba93 commit 75457dd

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Xcodes/Backend/AppState.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class AppState: ObservableObject {
179179
let username = Current.defaults.string(forKey: "username") {
180180
// remove any keychain password if we fail to log with an invalid username or password so it doesn't try again.
181181
try? Current.keychain.remove(username)
182+
Current.defaults.removeObject(forKey: "username")
182183
}
183184

184185
// This error message is not user friendly... need to extract some meaningful data in the different cases

Xcodes/Frontend/Preferences/GeneralPreferencePane.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ struct GeneralPreferencePane: View {
1313
Preferences.Container(contentWidth: 400.0) {
1414
Preferences.Section(title: "Apple ID") {
1515
VStack(alignment: .leading) {
16-
if appState.authenticationState == .authenticated {
17-
Text(Current.defaults.string(forKey: "username") ?? "-")
16+
// If we have saved a username then we will show it here,
17+
// even if we don't have a valid session right now,
18+
// because we should be able to get a valid session if needed with the password in the keychain
19+
// and a 2FA code from the user.
20+
// Note that AppState.authenticationState is not necessarily .authenticated in this case, though.
21+
if let username = Current.defaults.string(forKey: "username") {
22+
Text(username)
1823
Button("Sign Out", action: appState.signOut)
1924
} else {
2025
Button("Sign In", action: { self.appState.presentingSignInAlert = true })

0 commit comments

Comments
 (0)