Files
yattee/Yattee/Views/Settings/AccountSignInHandler.swift
Arkadiusz Fal 69179c9fe9 Give every login a screen, including the ones with nowhere to go
An account whose source is gone was listed nowhere: `orphanedAccounts` had
no readers, and the credential behind it could not be reached at all —
`AppEnvironment.accountCredentialsManager(for:)` resolves the store from
the account's instance, and an orphan has none. The new accounts screen
lists them and removes them by account ID, asking every account-keyed
store because the backend is no longer knowable. The pre-revamp
instance-keyed item is deliberately left behind: identifying it needs the
instance too.

Deleting a source has always signed out and removed the accounts on it.
Both confirmations now say so, and name them.

The sign-in that files a credential against an account is now one
implementation instead of a private method on the source editor, so the
accounts screen and the new optional sign-in step after adding a source
attach a login exactly as the editor does. Extracted verbatim — the
identity-scope guard, the account re-resolve, the write-then-read-back
and the rollback are what keep a half-written login from looking real.

Signing out from the per-instance section now asks first, and both
sign-out paths run one cascade rather than two copies of it.
2026-09-04 23:35:51 +02:00

105 lines
4.7 KiB
Swift

//
// AccountSignInHandler.swift
// Yattee
//
// What happens after a login succeeds, for every screen that offers one.
//
import Foundation
/// Files a successful login against an account, creating one if this was a
/// fresh sign-in.
///
/// One implementation for every place a login can be started — the per-instance
/// accounts section, the accounts screen, and adding a source — because the
/// order it does things in is what keeps a half-written login from looking like
/// a real one, and that order is not something a second copy would preserve.
@MainActor
struct AccountSignInHandler {
let appEnvironment: AppEnvironment
/// The instance being signed in to. A new account is created on it; an
/// existing one is expected to already live there.
let instance: Instance
/// The identifier is stored beside the credential in the Keychain rather
/// than on the account record, so it never travels as synced metadata.
/// - Returns: whether the credential was stored. A login the server
/// accepted is not the same as one this device could keep.
@discardableResult
func storeLogin(credential: String, username: String, target: AccountLoginTarget) -> Bool {
// Credential storage refuses to write under an unresolved identity
// scope, so creating the account first would leave one with no
// credential — and in a namespace that is discarded the moment the real
// scope arrives. Fail visibly instead.
guard appEnvironment.credentialIdentityScope.isResolved else {
appEnvironment.toastManager.showError(
String(localized: "accounts.signIn.identityUnresolved")
)
return false
}
// Re-resolved rather than trusted: signing in takes a round trip, and
// the account can be deleted on another device while it is happening.
// Writing the credential then would file a secret under an account that
// no longer exists, and report success for it.
if let existing = target.account, appEnvironment.accountsManager.account(id: existing.id) == nil {
appEnvironment.toastManager.showError(
String(localized: "accounts.signIn.storeFailed")
)
return false
}
let account = target.account
?? appEnvironment.accountsManager.addAccount(instanceID: instance.id)
guard let credentials = appEnvironment.accountCredentialsManager(for: account) else {
// Its instance may have gone in the meantime, which is what makes
// the backend unresolvable. A provisional account created moments
// ago must not be left behind.
if target.account == nil {
appEnvironment.accountsManager.rollbackAccountCreation(account.id)
}
return false
}
credentials.setCredential(credential, for: account)
// Confirm the write landed before reporting success: a Keychain failure
// would otherwise leave an account that looks signed in but has nothing
// behind it.
guard credentials.credential(for: account) != nil else {
if target.account == nil {
// Rolled back, not deleted: creating it also queued the marker
// that stops other devices synthesizing an account for this
// instance's pre-revamp login, and a sign-in that never
// completed must not leave that behind.
appEnvironment.accountsManager.rollbackAccountCreation(account.id)
}
appEnvironment.toastManager.showError(
String(localized: "accounts.signIn.storeFailed")
)
return false
}
if !username.isEmpty {
credentials.setUsername(username, for: account)
}
appEnvironment.accountsManager.setStatus(.signedIn, for: account.id)
// `usesAccountLogin` is deliberately not set. It is pre-revamp state
// that migration only reads, and it syncs through iCloud key-value
// storage independently of the account records — a device receiving the
// flag before the account would synthesize a migrated account for a
// login that was never legacy, and the marker that arrives later does
// not retract it. Accounts describe logins now; the flag only describes
// the world before they existed.
// Signing in again keeps the same account, so nothing downstream sees a
// change of context — but the credential behind it may now belong to
// somebody else, and their data must not survive the swap.
appEnvironment.accountCredentialDidChange(for: account)
return true
}
}