This SwiftUI app lets users enter email and password. They can tap Sign Up to create an account or Sign In to log in. The app shows messages about success or errors below the buttons.
import SwiftUI
import FirebaseAuth
struct ContentView: View {
@State private var email = ""
@State private var password = ""
@State private var message = ""
var body: some View {
VStack(spacing: 20) {
TextField("Email", text: $email)
.textContentType(.emailAddress)
.keyboardType(.emailAddress)
.autocapitalization(.none)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
SecureField("Password", text: $password)
.padding()
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
Button("Sign Up") {
Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
if let error = error {
message = "Sign up failed: \(error.localizedDescription)"
} else {
message = "Signed up as: \(authResult?.user.email ?? "No Email")"
}
}
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
Button("Sign In") {
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let error = error {
message = "Sign in failed: \(error.localizedDescription)"
} else {
message = "Signed in as: \(authResult?.user.email ?? "No Email")"
}
}
}
.padding()
.background(Color.green)
.foregroundColor(.white)
.cornerRadius(8)
Text(message)
.padding()
.foregroundColor(.red)
}
.padding()
}
}