Add authentication to app
This commit is contained in:
parent
2707a0d982
commit
fc0d177827
1 changed files with 48 additions and 1 deletions
|
@ -6,12 +6,59 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import LocalAuthentication
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct passwordApp: App {
|
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 {
|
var body: some Scene {
|
||||||
WindowGroup {
|
WindowGroup {
|
||||||
ListView(viewModel: ListViewModel())
|
VStack {
|
||||||
|
if isAuthenticated {
|
||||||
|
ListView(viewModel: ListViewModel())
|
||||||
|
} else {
|
||||||
|
Button("Authenticate") {
|
||||||
|
authenticate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onAppear {
|
||||||
|
authenticate()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue