I am trying to make an app in swift where users can add meals to a meal plan and generate a randomized meal plan. For storing the meals data so that users can save their meals when they close and reopen the app, I am using Google Firebase and Realtime Database. When the user opens the app, the app checks if the user exists in the database, and loads the data for that user if they do. However, when the user first launches the app, they do not have a uid, so I am trying to sign them in anonymously so that a new user is created and they can store their meals in the database. My code looks like this right now.
import Foundation
import FirebaseDatabase
import FirebaseAuth
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var mealNameField: UITextField!
@IBOutlet weak var mealIngredientsField: UITextField!
@IBOutlet weak var mealCountField: UITextField!
@IBOutlet weak var mealPlanScrollView: UIScrollView!
private let database = Database.database().reference()
private let auth = Auth.auth()
var meals: [String] = []
var mealIngredients: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
if let user = auth.currentUser {
let userId = user.uid
database.child("users").child(userId).child("meals").observeSingleEvent(of: .value, with: {
snapshot in
guard snapshot.value is [String] else {
return
}
self.meals = snapshot.value as! [String]
})
database.child("users").child(userId).child("mealIngredients").observeSingleEvent(of: .value, with: {
snapshot in
guard snapshot.value is [String] else {
return
}
self.mealIngredients = snapshot.value as! [String]
})
}
else {
print("Tring to sign in anonymously")
auth.signInAnonymously { authResult, error in
if let error = error {
print("Failed to sign in anonymously: \(error.localizedDescription)")
return
}
guard let user = authResult?.user else {
print("Failed to retrieve user information after anonymous sign-in.")
return
}
let userId = user.uid
print("here")
self.database.child("users").child(userId).child("meals").setValue(self.meals)
self.database.child("users").child(userId).child("mealIngredients").setValue(self.mealIngredients)
}
}
}
@IBAction func addMeal(_ sender: Any) {
if let mealName = mealNameField.text, let mealIngredients = mealIngredientsField.text {
if mealName != "" && mealIngredients != "" {
self.meals.append(mealName)
self.mealIngredients.append(mealIngredients)
mealNameField.text = ""
mealIngredientsField.text = ""
guard let currentUser = auth.currentUser else {
return
}
let userId = currentUser.uid
let userRef = database.child("users").child(userId)
userRef.child("meals").setValue(self.meals)
userRef.child("mealIngredients").setValue(self.mealIngredients)
}
}
}
The print statement inside of the let error = error prints out when I simulate the app. It prints:
Tring to sign in anonymously
Failed to sign in anonymously: An internal error has occurred, print and inspect the error details for more information.
I have made sure that the database is connected to my project, and if I add something to the database in the beginning of viewDidLoad, it works. I am not sure why this error is happening, or what I am doing wrong, so it would be great if someone could help me with this.
The full error is:
Failed to sign in anonymously: Error Domain=FIRAuthErrorDomain Code=17999 "An internal error has occurred, print and inspect the error details for more information." UserInfo={FIRAuthErrorUserInfoNameKey=ERROR_INTERNAL_ERROR, NSLocalizedDescription=An internal error has occurred, print and inspect the error details for more information., NSUnderlyingError=0x6000025407e0 {Error Domain=FIRAuthInternalErrorDomain Code=3 "(null)" UserInfo={NSUnderlyingError=0x6000025b8450 {Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data={length = 220, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 5d0a2020 7d0a7d0a }, data_content_type=application/json; charset=UTF-8}}, FIRAuthErrorUserInfoDeserializedResponseKey={ code = 400; errors = ( { domain = global; message = "CONFIGURATION_NOT_FOUND"; reason = invalid; }); message = "CONFIGURATION_NOT_FOUND";