-
-
Save hendore/fcdb25e2269c4e054aacbc9b7be6d6d3 to your computer and use it in GitHub Desktop.
SwiftUI Login Screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Credentials { | |
var email: String = "" | |
var password: String = "" | |
} | |
func authenticate(withCredentials credentials: Credentials, completion: @escaping (String?, Error?) -> Void) { | |
let session = URLSession(configuration: .default) | |
let endpoint = URL(string: "http://localhost:3000/token") | |
session.dataTask(with: endpoint!) { data, response, error in | |
DispatchQueue.main.async { | |
completion("example-token-need-to-pluck-it-from-data-or-response???", error) | |
} | |
}.resume() | |
} | |
struct Login: View { | |
@State private var credentials = Credentials() | |
@State private var error: Error? = nil | |
func attemptLogin() { | |
self.error = nil | |
authenticate(withCredentials: self.credentials, completion: { accessToken, error in | |
self.error = error | |
self.credentials.password = "" | |
// now check if accessToken is available, if so what do I do with it here? | |
// also, how do we then navigate to another screen from here (without a button action 🤔) | |
}) | |
} | |
var body: some View { | |
VStack(spacing: 30) { | |
TextField("Email", text: $credentials.email) | |
.padding() | |
.background(Color.white) | |
.clipShape(RoundedRectangle(cornerRadius: 5)) | |
SecureField("Password", text: $credentials.password) | |
.padding() | |
.background(Color.white) | |
.clipShape(RoundedRectangle(cornerRadius: 5)) | |
Button(action: self.attemptLogin) { | |
Text("Login") | |
} | |
.padding(10) | |
.frame(width: UIScreen.main.bounds.width / 2) | |
.background(Color.accentColor) | |
.foregroundColor(Color.white) | |
.cornerRadius(5) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment