Added TouchID/FaceID to sign in

This commit is contained in:
Isaac Greene 2022-06-11 22:38:35 -04:00
parent 5c4d4f8059
commit 7d6643112d
12 changed files with 239 additions and 52 deletions

View file

@ -6,9 +6,10 @@
//
import SwiftUI
// this file will not have comments.
// this code is considered simple enough to be
// human-readable without aid, as long as
import LocalAuthentication
// this file will have some comments.
// Most of this code is considered simple enough
// to be human-readable without aid, as long as
// the reader has a basic understanding of
// Swift and/or SwiftUI.
@ -23,7 +24,8 @@ struct DocsView: View {
@State private var pass: String = ""
@State private var user: String = ""
@FocusState private var focusedField: Field?
@State private var isUnlocked = false
@FocusState private var focusedField: Field?
var body: some View {
NavigationView {
@ -44,24 +46,38 @@ struct DocsView: View {
}
Section(header: Text("App Information")) {
NavigationLink("Software License", destination: LicenseView())
Text("Version: Release Candidate (1.0.0)")
Text("Release date: 2022-06-10")
Text("Version: Release Candidate 2 (1.0.0)")
Text("Release date: 2022-06-11")
Text("Start date: 2022-03-25")
Link("Built with SwiftUI \(Image(systemName: "swift"))", destination: URL(string: "https://developer.apple.com/xcode/swiftui")!)
}
Section(header: Text("Login")) {
SecureField("Username", text: $user)
.keyboardType(.alphabet)
.textContentType(.username)
.submitLabel(.next)
.focused($focusedField, equals: .username)
SecureField("Password", text: $pass)
.keyboardType(.numbersAndPunctuation)
.textContentType(.password)
.submitLabel(.done)
.focused($focusedField, equals: .password)
if (pass == password && user == username) {
if (isUnlocked) {
NavigationLink("Contacts", destination: SecretView())
Button("Log out") {
pass = ""
user = ""
isUnlocked = false
}
} else {
if !isUnlocked {
Button("Log in with biometrics") {
authenticate()
}
}
SecureField("Username", text: $user)
.keyboardType(.alphabet)
.textContentType(.username)
.submitLabel(.next)
.focused($focusedField, equals: .username)
SecureField("Password", text: $pass)
.keyboardType(.numbersAndPunctuation)
.textContentType(.password)
.submitLabel(.done)
.focused($focusedField, equals: .password)
if checkPassword() {
NavigationLink("Contacts", destination: SecretView())
}
}
}
}
@ -74,8 +90,48 @@ struct DocsView: View {
default:
()
}
if (pass == password && user == username) {
isUnlocked = true
}
}
}
func checkPassword() -> Bool {
if (pass == password && user == username) {
return true
} else {
return false
}
}
func checkIfUnlocked() {
if !isUnlocked {
authenticate()
}
}
func authenticate() {
let context = LAContext()
var error: NSError?
// check whether biometric authentication is possible
if context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
// it's possible, so go ahead and use it
let reason = "We need authentication before we can show you sensitive data"
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: reason) { success, authenticationError in
// authentication has now completed
if success {
isUnlocked = true
} else {
()
}
}
} else {
()
}
}
// getting this to work came from
// https://www.hackingwithswift.com/books/ios-swiftui/using-touch-id-and-face-id-with-swiftui
// a truly epic website and it's helped me with
// just about all of my code questions
}
struct DocsView_Previews: PreviewProvider {