Here is the code. I am meant to for from RegistrationView to the ProfilePhotoSelectorView(), but it shows up then disappears.
import SwiftUI
import Firebase
class AuthViewModel: ObservableObject {
@Published var userSession: FirebaseAuth.User?
@Published var didAuthenticateUser = false
init() {
self.userSession = Auth.auth().currentUser
print("Debug: User session is \(self.userSession?.uid)")
}
func login(withEmail email: String, password: String){
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if let error = error {
print("Debug: Failed to sign in with error \(error.localizedDescription)")
return
}
guard let user = result?.user else {return}
self.userSession = user
print("Debug: Did log user in")
}
}
func register(withEmail email: String, password: String, fullname: String, username: String){
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if let error = error {
print("Debug: Failed to register with error \(error.localizedDescription)")
return
}
guard let user = result?.user else {return}
print("Debug: Registered user successfully")
print("Debug: User is \(self.userSession)")
let data = [
"email" : email,
"username" : username.lowercased(),
"fullname" : fullname,
"uid" : user.uid
]
Firestore.firestore().collection("users")
.document(user.uid)
.setData(data){ _ in
self.didAuthenticateUser = true
}
}
}
func signOut() {
userSession = nil
try? Auth.auth().signOut()
}
}
import SwiftUI
struct RegistrationView: View {
@State private var email = ""
@State private var username = ""
@State private var fullname = ""
@State private var password = ""
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var viewModel: AuthViewModel
var body: some View {
VStack{
NavigationLink(destination: ProfilePhotoSelectorView(),
isActive: $viewModel.didAuthenticateUser,
label: {})
AuthenticationHeaderView(title1: "Get started", title2: "Create an account")
VStack(spacing: 40){
CustomInputField(imageName: "envelope",
placeHolderText: "Email",
text: $email)
CustomInputField(imageName: "person",
placeHolderText: "Username",
text: $username)
CustomInputField(imageName: "person",
placeHolderText: "Full name",
text: $fullname)
CustomInputField(imageName: "lock",
placeHolderText: "Password",
isSecureField: true,
text: $password)
}
.padding(32)
Button{
viewModel.register(withEmail: email, password: password, fullname: fullname, username: username)
} label: {
Text("Sign Up")
.font(.headline)
.foregroundColor(.white)
.frame(width: 340, height: 50)
.background(Color(.systemBlue))
.clipShape(Capsule())
.padding(.top, 15)
.padding(.bottom, 20)
}
.shadow(color: .blue.opacity(0.7), radius: 10, x: 0, y: 0)
Spacer()
Button {
presentationMode.wrappedValue.dismiss()
} label: {
HStack{
Text("Already have an account?")
.font(.footnote)
Text("Sign In")
.font(.footnote)
.fontWeight(.semibold)
}
}
.padding(.bottom, 32)
.foregroundColor(Color(.systemBlue))
}.ignoresSafeArea()
}
}
struct RegistrationView_Previews: PreviewProvider {
static var previews: some View {
RegistrationView()
}
}
struct ProfilePhotoSelectorView: View {
var body: some View {
VStack{
AuthenticationHeaderView(title1: "Create your account",
title2: "Add a profile photo")
Button {
print("Image Here")
} label: {
Image(systemName: "plus")
.resizable()
.frame(width: 130, height: 130)
.padding(.top, 40)
}
Spacer()
}
.ignoresSafeArea()
}
}
struct ProfilePhotoSelectorView_Previews: PreviewProvider {
static var previews: some View {
ProfilePhotoSelectorView()
}
}
I think i’ve got the necessary code copied and pasted. let me know if theres any information i missed out please