var authError: Error? return Observable<String>.create({ observer in Auth.auth().addStateDidChangeListener({ _, user in guard let user = user else { authError = FirebaseAuthError.unknownUser semaphore.signal() return } user.getIDTokenForcingRefresh(true, completion: { token, error in if error != nil { authError = FirebaseAuthError.noAuthToken semaphore.signal() return } UserCredentials.shared.accessToken = token semaphore.signal() }) }) semaphore.wait() if authError != nil { observer.onError(authError!) } observer.onNext(UserCredentials.shared.accessToken ?? "") observer.onCompleted() return Disposables.create() }) }