@@ -27,6 +27,7 @@ public class AccountSwitcher: NSObject, ObservableObject {
2727 /// A cache for storing decoded tokens from UserDefaults
2828 private var tokens : [ String : String ] = [ : ]
2929
30+ // MARK: - Account metadata methods
3031 func loadAccounts( ) {
3132 guard let dec = try ? JSONDecoder ( ) . decode (
3233 [ AccountMeta ] . self,
@@ -39,7 +40,11 @@ public class AccountSwitcher: NSObject, ObservableObject {
3940 func writeAccounts( ) {
4041 UserDefaults . standard. setValue ( try ? JSONEncoder ( ) . encode ( accounts) , forKey: AccountSwitcher . META_KEY)
4142 }
43+ func removeAccount( for id: Snowflake ) {
44+ accounts. removeAll ( identifiedBy: id)
45+ }
4246
47+ // MARK: - Secure token storage methods
4348 func saveToken( for id: Snowflake , token: String ) {
4449 tokens [ id] = token
4550 // Keychain.save(key: "\(SwiftcordApp.tokenKeychainKey).\(id)", data: token)
@@ -84,34 +89,45 @@ public class AccountSwitcher: NSObject, ObservableObject {
8489 }
8590 func logOut( id: Snowflake ) async {
8691 guard let token = getToken ( for: id) else { return }
87- await DiscordREST ( token: token) . logOut ( )
88- Keychain . remove ( key: " \( SwiftcordApp . tokenKeychainKey) . \( id) " )
92+ removeToken ( for: id)
8993
9094 DispatchQueue . main. async { [ weak self] in
9195 withAnimation {
92- self ? . accounts . removeAll { $0 . id == id }
96+ self ? . removeAccount ( for : id )
9397 self ? . writeAccounts ( )
9498
9599 // Actions to take if the account being logged out is the current one
96100 if UserDefaults . standard. string ( forKey: AccountSwitcher . ACTIVE_KEY) == id {
97- AccountSwitcher . clearAccountSpecificPrefKeys ( )
98- if let firstID = self ? . accounts. first? . id {
99- self ? . setActiveAccount ( id: firstID)
100- } else {
101- UserDefaults . standard. removeObject ( forKey: AccountSwitcher . ACTIVE_KEY)
102- }
101+ self ? . setActiveAccount ( id: self ? . accounts. first? . id)
103102 }
104103 }
105104 }
105+
106+ await DiscordREST ( token: token) . logOut ( )
107+ }
108+ /// Mark the current user as invalid - i.e. remove it from the token store and acc
109+ ///
110+ /// The next account (if present) will automatically become "active" after invalidating the current one.
111+ /// > Note: The user will not be signed out from the Discord API
112+ func invalidate( ) {
113+ guard let id = getActiveID ( ) else { return }
114+ removeToken ( for: id)
115+ removeAccount ( for: id)
116+ print ( " has accounts? \( !accounts. isEmpty) " )
117+ setActiveAccount ( id: accounts. first? . id)
106118 }
107119
108120 func setPendingToken( token: String ) {
109121 pendingToken = token
110122 }
111- func setActiveAccount( id: Snowflake ) {
112- // ID is always assumed to be correct
113- UserDefaults . standard. set ( id, forKey: AccountSwitcher . ACTIVE_KEY)
123+ func setActiveAccount( id: Snowflake ? ) {
114124 AccountSwitcher . clearAccountSpecificPrefKeys ( ) // Clear account specific UserDefault keys
125+ if let id = id {
126+ // ID is always assumed to be correct
127+ UserDefaults . standard. set ( id, forKey: AccountSwitcher . ACTIVE_KEY)
128+ } else {
129+ UserDefaults . standard. removeObject ( forKey: AccountSwitcher . ACTIVE_KEY)
130+ }
115131 }
116132
117133 // Multiple sanity checks ensure account meta is valid, if not, repair is attempted
@@ -121,7 +137,7 @@ public class AccountSwitcher: NSObject, ObservableObject {
121137 AccountSwitcher . log. info ( " Found token in old key! Logging in with this token... " )
122138 return oldToken
123139 }
124- let storedActiveID = UserDefaults . standard . string ( forKey : AccountSwitcher . ACTIVE_KEY )
140+ let storedActiveID = getActiveID ( )
125141 guard let activeID = storedActiveID != nil && accounts. contains ( where: { $0. id == storedActiveID } )
126142 ? storedActiveID
127143 : accounts. first? . id else { return nil } // Account not signed in
@@ -133,6 +149,7 @@ public class AccountSwitcher: NSObject, ObservableObject {
133149 }
134150 return token
135151 }
152+ func getActiveID( ) -> String ? { UserDefaults . standard. string ( forKey: AccountSwitcher . ACTIVE_KEY) }
136153
137154 func onSignedIn( with user: CurrentUser ) {
138155 // Migrate from old keychain key to new keys
0 commit comments