firebase-ios-sdk
firebase-ios-sdk copied to clipboard
Failed to get document because the client is offline.
Description
Can't login to the app, because I'm getting this issue Failed to get document because the client is offline. RegisterView doesn't work too, because the button doesn't do anything. There is problem with login, but password reseting works fine. App is coded in SwiftUI.
Reproducing the issue
Try to login to the app with email and password, which is in the database.
Firebase SDK Version
10.29
Xcode Version
16.1
Installation Method
Swift Package Manager
Firebase Product(s)
Authentication, Firestore, Storage
Targeted Platforms
iOS
Relevant Log Output
// LoginView.swift
// opensocial
//
// Created by Filip Bukovina on 21.06.2024.
//
import SwiftUI
import Firebase
import FirebaseFirestore
import FirebaseStorage
import FirebaseCore
import FirebaseAuth
struct LoginView: View {
// MARK: User Details
@State var emailID: String = ""
@State var password: String = ""
// MARK: View Properties
@State var createAccount: Bool = false
@State var showError: Bool = false
@State var errorMessage: String = ""
@State var isLoading: Bool = false
// MARK: User Defaults
@AppStorage("user_profile_url") var profileURL: URL?
@AppStorage("user_name") var userNameStored: String = ""
@AppStorage("user_UID") var userUID: String = ""
@AppStorage("log_status") var logStatus: Bool = false
var body: some View {
VStack(spacing: 10){
Text("Hello again!")
.font(.largeTitle.bold())
.hAlign(.leading)
Text("Welcome Back,\nWe're happy, that you are back.")
.font(.title3)
.hAlign(.leading)
Text("Version 0.1(9)")
.font(.caption)
.hAlign(.leading)
VStack(spacing: 12){
TextField("Email", text: $emailID)
.textContentType(.emailAddress)
.border(1, .gray.opacity(0.5))
.padding(.top,25)
SecureField("Password", text: $password)
.textContentType(.emailAddress)
.border(1, .gray.opacity(0.5))
Button("Reset password?", action: resetPassword)
.font(.callout)
.fontWeight(.medium)
.tint(.black)
.hAlign(.trailing)
Button(action: loginUser){
// MARK: Login Button
Text("Sign in")
.foregroundColor(.white)
.hAlign(.center)
.fillView(.black)
}
.padding(.top,10)
}
// MARK: Register Button
HStack{
Text("Ready to use secure social?")
.foregroundColor(.gray)
Button("Register Now"){
createAccount.toggle()
}
.fontWeight(.bold)
.foregroundColor(.black)
}
.font(.callout)
.vAlign(.bottom)
}
.vAlign(.top)
.padding(15)
.overlay(content: {
LoadingView(show: $isLoading)
})
// MARK: Register View VIA Sheets
.fullScreenCover(isPresented: $createAccount) {
RegisterView()
}
// MARK: Displaying Alert
.alert(errorMessage, isPresented: $showError, actions: {})
}
func loginUser(){
isLoading = true
closeKeyboard()
Task{
do{
// With the help of Swift Concurrency Auth can be done with Single Line
try await Auth.auth().signIn(withEmail: emailID, password: password)
print("User Found")
}catch{
await setError(error)
}
}
}
// MARK: If User if Found then Fetching User Data From Firestore
func fetchUser()async throws{
guard let userID = Auth.auth().currentUser?.uid else{return}
let user = try await Firestore.firestore().collection("Users").document(userID).getDocument(as: User.self)
// MARK: UI Updating Must be Run On Main Thread
await MainActor.run(body: {
// Setting UserDefaults data and Changing App's Auth Status
userUID = userID
userNameStored = user.username
profileURL = user.userProfileURL
logStatus = true
})
}
func resetPassword(){
Task{
do{
// With the help of Swift Concurrency Auth can be done with Single Line
try await Auth.auth().sendPasswordReset(withEmail: emailID)
print("Link Sent")
}catch{
await setError(error)
}
}
}
// MARK: Displaying Errors VIA Alert
func setError(_ error: Error)async{
// MARK: UI Must be Updated on Main Thread
await MainActor.run(body: {
errorMessage = error.localizedDescription
showError.toggle()
isLoading = false
})
}
}
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
If using Swift Package Manager, the project's Package.resolved
Expand Package.resolved snippet
Replace this line with the contents of your Package.resolved.
If using CocoaPods, the project's Podfile.lock
Expand Podfile.lock snippet
Replace this line with the contents of your Podfile.lock!
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Thanks for reporting @FBukovina. Your application seems to be doing a simple getDocument call and getting an UNAVAILABLE response
https://github.com/firebase/firebase-ios-sdk/blob/b91dfda0264195cfa47c0dea3b08f2184327c4d0/Firestore/core/src/api/document_reference.cc#L156-L158
Are you consistently getting this error?
It happens all the time. So I should this to my code? Thanks!
So I should this to my code?
No, I was just highlighting the fact that the error you're seeing is a result of getting an "unavailable" error code. Are you able to perform any Firestore operation? Can your device reach the network? Are you targeting the Firestore backend or emulator? Do your security rules allow access to the document you are trying to access?
I can reset password, I get the email with link and everything works fine. I tried it in iOS simulator, physical iPhone 13 and 16 Plus with same error. Firebase is set in production mod. Device can reach network.
Hi @FBukovina ,
- Are you able to perform any Firestore operation?
- Do Firestore operations succeed if you target the Firestore emulaator?
- Do your security rules allow access to the document you are trying to access?
Hey @FBukovina. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
Hello, what do you mean with Firestore operation?
By Firestore operation, I mean reads, writes, updates, and deletes.
Hey @FBukovina. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
I can restore password via the app, but registration and login doesn't work. I can create a user account in Firebase dashboard, but I can't login with it.
Hi there, I'm closing this issue due to inactivity. If this issue is still being encountered on the latest Firebase major version (v12), please let me know and I'd be happy to reopen.