From fc0d177827dfab1bb0f39e7f2331ea029e474a73 Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Thu, 16 Jan 2025 16:18:29 +0100 Subject: [PATCH] Add authentication to app --- password/passwordApp.swift | 49 +++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/password/passwordApp.swift b/password/passwordApp.swift index 9ae8896..8b32159 100644 --- a/password/passwordApp.swift +++ b/password/passwordApp.swift @@ -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() + } } } }