Files
NetNewsWire/iOS/Account/MinifluxAccountView.swift

151 lines
5.2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// MinifluxAccountView.swift
// NetNewsWire-iOS
//
// Created by Dave Marquard on 7/15/26.
// Copyright © 2026 Ranchero Software. All rights reserved.
//
import SwiftUI
import RSCore
import Account
import Secrets
/// Miniflux supports either a username and password or an API token.
struct MinifluxAccountView: View {
let account: Account?
let didAddAccount: (() -> Void)?
@Environment(\.dismiss) private var dismiss
@State private var username = ""
@State private var password = ""
@State private var apiToken = ""
@State private var apiURLString = ""
@State private var usesAPIToken = false
@State private var isValidating = false
@State private var errorMessage: String?
@State private var isShowingSignUp = false
var body: some View {
AccountSetupSheet(accountType: .miniflux, isWorking: isValidating) {
Section {
Picker(NSLocalizedString("Authentication", comment: "Authentication"), selection: $usesAPIToken) {
Text(NSLocalizedString("Password", comment: "Password")).tag(false)
Text(NSLocalizedString("API Token", comment: "API Token")).tag(true)
}
if usesAPIToken {
SecureField(NSLocalizedString("API Token", comment: "API Token"), text: $apiToken)
} else {
TextField(NSLocalizedString("Username", comment: "Username"), text: $username)
.textContentType(.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
SecureField(NSLocalizedString("Password", comment: "Password"), text: $password)
.textContentType(.password)
}
TextField(NSLocalizedString("API URL: https://miniflux.example.com", comment: "Miniflux API Helper"), text: $apiURLString)
.textContentType(.URL)
.keyboardType(.URL)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
} header: {
AccountIconHeader(accountType: .miniflux)
}
Section {
Button(account == nil ? NSLocalizedString("Add Account", comment: "Add Account") : NSLocalizedString("Update Credentials", comment: "Update Credentials")) {
Task {
await submit()
}
}
.frame(maxWidth: .infinity)
.disabled(!canSubmit || isValidating)
} footer: {
AccountSheetFooter(text: NSLocalizedString("Sign in to your Miniflux instance and sync your feeds across your devices. Your credentials will be stored securely in your keychain.\n\nDon’t have a Miniflux instance?", comment: "Miniflux"), linkTitle: NSLocalizedString("Find Out More", comment: "Find Out More")) {
isShowingSignUp = true
}
}
}
.errorAlert(message: $errorMessage)
.sheet(isPresented: $isShowingSignUp) {
if let signUpURL = URL(string: "https://miniflux.app") {
SafariView(url: signUpURL)
}
}
.onAppear {
loadExistingCredentials()
}
}
private var canSubmit: Bool {
guard !apiURLString.trimmingWhitespace.isEmpty else {
return false
}
return usesAPIToken
? !apiToken.trimmingWhitespace.isEmpty
: !username.trimmingWhitespace.isEmpty && !password.isEmpty
}
private func loadExistingCredentials() {
guard let account else {
return
}
apiURLString = account.endpointURL?.absoluteString ?? ""
if let credentials = try? account.retrieveCredentials(type: .minifluxAPIToken) {
usesAPIToken = true
apiToken = credentials.secret
} else if let credentials = try? account.retrieveCredentials(type: .minifluxBasic) {
username = credentials.username
password = credentials.secret
}
}
@MainActor private func submit() async {
guard let endpoint = Miniflux.normalizedEndpointURL(from: apiURLString) else {
errorMessage = NSLocalizedString("Invalid API URL.", comment: "Invalid API URL")
return
}
let credentials = usesAPIToken
? Credentials(type: .minifluxAPIToken, username: "", secret: apiToken.trimmingWhitespace)
: Credentials(type: .minifluxBasic, username: username.trimmingWhitespace, secret: password)
isValidating = true
defer {
isValidating = false
}
do {
guard let validatedCredentials = try await Account.validateCredentials(type: .miniflux, credentials: credentials, endpoint: endpoint) else {
errorMessage = NSLocalizedString("Invalid username/password combination, or invalid API token.", comment: "Credentials Error")
return
}
if account == nil && AccountManager.shared.duplicateServiceAccount(type: .miniflux, username: validatedCredentials.username, endpoint: endpoint) {
errorMessage = NSLocalizedString("There is already a Miniflux account for this user on this server.", comment: "Duplicate Error")
return
}
let account = self.account ?? AccountManager.shared.createAccount(type: .miniflux)
// The Keychain key comes from the endpoint and username, so remove the old credentials before either changes.
try? account.removeCredentials(type: .minifluxBasic)
try? account.removeCredentials(type: .minifluxAPIToken)
account.endpointURL = endpoint
do {
try account.storeCredentials(validatedCredentials)
} catch {
if self.account == nil {
AccountManager.shared.deleteAccount(account)
}
errorMessage = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
return
}
account.triggerRefreshAll()
dismiss()
didAddAccount?()
} catch {
errorMessage = error.localizedDescription
}
}
}