Reputation: 469
I am making a Food Delivery App with SwiftUI and Firebase, and I have been able to implement Email/Password Authentication. I have a ProfileView to display a User's Name and the vendors that they own. However when I try to access the user's displayName property it just returns an empty String. Is there any way to access or change a user's Display name from the Firebase Console.
ProfileView.swift
struct ProfileView: View {
var user: User?
@ObservedObject var vendorRepo = VendorRepository()
var body: some View {
VStack(alignment: .center) {
Text("Profile")
.font(.largeTitle)
.bold()
ImageView(imageName: "examplePP", width: 100, height: 100)
Text(user?.displayName ?? "No Username Provided")
.font(.title)
Divider()
NavigationView {
List(vendorRepo.userOwnedVendors) { vendor in
NavigationLink(destination: VendorView(vendor: vendor)) {
VendorRow(vendor: vendor)
}
}
.navigationBarTitle(Text("Vendors Owned by You"), displayMode: .inline)
}
}
.padding(.top, -25.0)
.onAppear() {
self.vendorRepo.readUserOwnedVendors()
}
}
}
EmailSignUpView.swift
struct EmailSignUpView: View {
@State var displayName: String = ""
@State var email: String = ""
@State var password: String = ""
var body: some View {
VStack(alignment: .center, spacing: 20) {
VStack {
Text("Sign Up with Email")
.font(.largeTitle)
.bold()
Text("You must have an account to be able to start a vendor or modify your existing ones. If you already have an account, go to the Login Page.")
.multilineTextAlignment(.center)
}
Spacer()
VStack(alignment: .center) {
Text("Enter your Display Name:")
TextField("Display Name", text: $displayName)
.disableAutocorrection(true)
.autocapitalization(.none)
.multilineTextAlignment(.center)
}
VStack(alignment: .center) {
Text("Enter your Email:")
TextField("Email Address", text: $email)
.disableAutocorrection(true)
.autocapitalization(.none)
.multilineTextAlignment(.center)
}
VStack(alignment: .center) {
Text("Enter your Password:")
SecureField("Password", text: $password)
.disableAutocorrection(true)
.autocapitalization(.none)
.multilineTextAlignment(.center)
}
Spacer()
Button(action: createAccount) {
Text("Sign Up")
.font(.title)
}
Spacer()
}
.padding(.horizontal)
}
func createAccount() {
Auth.auth().createUser(withEmail: self.email, password: self.password, completion: {result, error in
var contentTitleString: String = ""
var contentSubtitleString: String = ""
if let error = error {
print(error)
contentTitleString = "Sign Up Failed!"
contentSubtitleString = error.localizedDescription
self.password = ""
} else {
print("\(self.email) has been signed up!")
contentTitleString = "Sign Up Successful!"
contentSubtitleString = "Please restart the app for changes to take place"
self.displayName = ""
self.email = ""
self.password = ""
if let currentUser = Auth.auth().currentUser?.createProfileChangeRequest() {
currentUser.displayName = self.displayName
currentUser.commitChanges(completion: {error in
if let error = error {
print(error)
} else {
print("DisplayName changed")
}
})
}
}
let content = UNMutableNotificationContent()
content.title = contentTitleString
content.subtitle = contentSubtitleString
content.sound = .default
// show this notification one seconds from now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
})
}
}
When I call the ProfileView View, I pass Auth.auth().currentUser
for the user
property.
Upvotes: 0
Views: 1108
Reputation: 469
I think I found the mistake. In the EmailSignUpView's createAcount() function:
func createAccount() {
Auth.auth().createUser(withEmail: self.email, password: self.password, completion: {result, error in
var contentTitleString: String = ""
var contentSubtitleString: String = ""
if let error = error {
print(error)
contentTitleString = "Sign Up Failed!"
contentSubtitleString = error.localizedDescription
self.password = ""
} else {
print("\(self.email) has been signed up!")
contentTitleString = "Sign Up Successful!"
contentSubtitleString = "Please restart the app for changes to take place"
self.displayName = ""
self.email = ""
self.password = ""
if let currentUser = Auth.auth().currentUser?.createProfileChangeRequest() {
currentUser.displayName = self.displayName
currentUser.commitChanges(completion: {error in
if let error = error {
print(error)
} else {
print("DisplayName changed")
}
})
}
}
let content = UNMutableNotificationContent()
content.title = contentTitleString
content.subtitle = contentSubtitleString
content.sound = .default
// show this notification one seconds from now
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
})
}
I set the displayName to ""
before updating. Oops!
Upvotes: 1