Add authentication to app

This commit is contained in:
Markus Thielker 2025-01-16 16:18:29 +01:00
parent 2707a0d982
commit fc0d177827
No known key found for this signature in database

View file

@ -6,12 +6,59 @@
//
import SwiftUI
import LocalAuthentication
@main
struct passwordApp: App {
@State private var isAuthenticated = false
func authenticate() {
let context = LAContext()
var error: NSError?
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
let reason = "This app displays passwords in clear text. Please authenticate using Touch ID or Face ID."
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
if success {
isAuthenticated = true
} else {
fallBackToPasscode()
}
}
} else {
fallBackToPasscode()
}
}
func fallBackToPasscode() {
let context = LAContext()
let reason = "This app displays passwords in clear text. Please authenticate using your passcode."
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, authenticationError in
if success {
isAuthenticated = true
} else {
// Handle passcode authentication error
}
}
}
var body: some Scene {
WindowGroup {
ListView(viewModel: ListViewModel())
VStack {
if isAuthenticated {
ListView(viewModel: ListViewModel())
} else {
Button("Authenticate") {
authenticate()
}
}
}
.onAppear {
authenticate()
}
}
}
}