From 75457dd9eb1a1dafc3718c46cab7115da1d60ea0 Mon Sep 17 00:00:00 2001 From: Brandon Evans Date: Mon, 25 Jan 2021 19:56:28 -0700 Subject: [PATCH] Show stored username in General preference pane This reverts the change from 90c067997bdd3e1e3a18c7896baa6d119fcc61ba 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. --- Xcodes/Backend/AppState.swift | 1 + Xcodes/Frontend/Preferences/GeneralPreferencePane.swift | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Xcodes/Backend/AppState.swift b/Xcodes/Backend/AppState.swift index 087bb10..49a6de1 100644 --- a/Xcodes/Backend/AppState.swift +++ b/Xcodes/Backend/AppState.swift @@ -179,6 +179,7 @@ class AppState: ObservableObject { let username = Current.defaults.string(forKey: "username") { // remove any keychain password if we fail to log with an invalid username or password so it doesn't try again. try? Current.keychain.remove(username) + Current.defaults.removeObject(forKey: "username") } // This error message is not user friendly... need to extract some meaningful data in the different cases diff --git a/Xcodes/Frontend/Preferences/GeneralPreferencePane.swift b/Xcodes/Frontend/Preferences/GeneralPreferencePane.swift index cef62a2..ac74ee7 100644 --- a/Xcodes/Frontend/Preferences/GeneralPreferencePane.swift +++ b/Xcodes/Frontend/Preferences/GeneralPreferencePane.swift @@ -13,8 +13,13 @@ struct GeneralPreferencePane: View { Preferences.Container(contentWidth: 400.0) { Preferences.Section(title: "Apple ID") { VStack(alignment: .leading) { - if appState.authenticationState == .authenticated { - Text(Current.defaults.string(forKey: "username") ?? "-") + // If we have saved a username then we will show it here, + // even if we don't have a valid session right now, + // because we should be able to get a valid session if needed with the password in the keychain + // and a 2FA code from the user. + // Note that AppState.authenticationState is not necessarily .authenticated in this case, though. + if let username = Current.defaults.string(forKey: "username") { + Text(username) Button("Sign Out", action: appState.signOut) } else { Button("Sign In", action: { self.appState.presentingSignInAlert = true })