Skip to content

Instantly share code, notes, and snippets.

@tgnivekucn
Created March 16, 2023 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgnivekucn/c43ed7ad3c4ec8daa44434a601c42f37 to your computer and use it in GitHub Desktop.
Save tgnivekucn/c43ed7ad3c4ec8daa44434a601c42f37 to your computer and use it in GitHub Desktop.
SwiftUI login page UI
import SwiftUI
import Auth0
struct LoginView: View {
@State private var email = ""
@State private var password = ""
@State private var profile: UserInfo?
var body: some View {
VStack {
if profile != nil {
Text("Welcome \(profile!.name ?? "user")")
Button("Logout") {
Auth0.webAuth().clearSession(federated: false) { _ in }
self.profile = nil
}
} else {
TextField("Email", text: $email)
.padding()
.border(Color.gray)
SecureField("Password", text: $password)
.padding()
.border(Color.gray)
Button("Login") {
Auth0.webAuth()
.scope("openid profile")
.audience("https://YOUR_DOMAIN/userinfo")
.start { result in
switch result {
case .success(let credentials):
Auth0.authentication()
.userInfo(withAccessToken: credentials.accessToken!)
.start { result in
switch result {
case .success(let profile):
self.profile = profile
case .failure(let error):
print(error)
}
}
case .failure(let error):
print(error)
}
}
}
}
}.padding()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment