Created
March 16, 2023 05:53
-
-
Save tgnivekucn/c43ed7ad3c4ec8daa44434a601c42f37 to your computer and use it in GitHub Desktop.
SwiftUI login page UI
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
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