Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to get document because the client is offline. #14107

Open
FBukovina opened this issue Nov 13, 2024 · 4 comments
Open

Failed to get document because the client is offline. #14107

FBukovina opened this issue Nov 13, 2024 · 4 comments
Assignees

Comments

@FBukovina
Copy link

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!
@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@ehsannas
Copy link
Contributor

Thanks for reporting @FBukovina. Your application seems to be doing a simple getDocument call and getting an UNAVAILABLE response

listener_->OnEvent(
Status{Error::kErrorUnavailable,
"Failed to get document because the client is offline."});

Are you consistently getting this error?

@FBukovina
Copy link
Author

It happens all the time.
So I should this to my code?
Thanks!

@ehsannas
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants