- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Angular 项目成功使用了 AngularFireAuth
可从 @angular/fire 注入(inject)连接到本地 Firebase 身份验证模拟器。我使用这些资源作为我的实现的引用:
constructor( private _fireauth: AngularFireAuth) {
this._fireAuth.useEmulator(`http://${location.hostname}:9099/`);
this._fireauth.setPersistence(firebase.auth.Auth.Persistence.LOCAL);
}
这没有任何区别。然后我在浏览器网络控制台中注意到,显然firebase库确实在页面加载后找到了本地存储的 session ,但最初尝试联系谷歌服务器来验证它,这当然失败了,因为它是一个本地模拟器 session :
_fireAuth.useEmulator()
仅在组件构造函数中调用。应用程序的其他部分,例如
AngularFireAuthGuard
可能会尝试更早地访问身份验证状态。几次尝试调用
useEmulator()
在应用程序的任何其他部分可能尝试访问身份验证状态失败之前:
Attempt1:
调用useEmulator()
在 @Injectable({providedIn: 'root'})
又名服务构造函数Attempt2:
提供 APP_INITIALIZER
在应用程序启动之前执行代码:export function initializeApp1(afa: AngularFireAuth): any {
return () => {
return new Promise(resolve => {
afa.useEmulator(`http://${location.hostname}:9099/`);
setTimeout(() => resolve(), 5000);
});
};
}
@NgModule({
declarations: [
...
],
imports: [
...,
AngularFireModule.initializeApp(environment.firebase),
AngularFireAuthModule,
...
],
providers: [
AngularFireAuth,
{
provide: APP_INITIALIZER,
useFactory: initializeApp1,
deps: [AngularFireAuth],
multi: true
}
],
bootstrap: [AppComponent]
})
Attempt3:
提供USE_EMULATOR
可注入(inject)的seems to be implemented在 AngularFireAuth
构造函数: providers: [
{
provide: USE_EMULATOR,
useValue: [location.hostname, 9099]
},
]
我特别想
Attempt3
会尽可能早地设置模拟器模式,因为它可以比在构造函数中设置早多少? Injectable 似乎可以工作,因为浏览器控制台会在页面加载时打印通常的警告消息:
auth.esm.js:133 WARNING: You are using the Auth Emulator, which is intended for local testing only. Do not use with production credentials.
.但是,对
googleapis.com
的请求每次在页面加载时检测到本地 indexedDB 中的存储 session 时仍然会触发,之后我会注销。另外值得注意的是:在
Attempt2
我使用 5 秒超时来显着减慢应用程序初始化和对
googleapis.com
的请求在页面加载后立即触发,在 Angular 完成初始化之前。
@angular/fire
is obviously build upon the basic Firebase JavaScript library ,所以我可能不得不更早地设置模拟器模式,但也许我在这里走错了方向?
Attempt 2
与
Attempt 3
有效,只要
initializeApp1()
方法使用
setTimout()
至少约。 100 毫秒。当我删除超时或将其设置为低至 10 毫秒时,登录就会丢失。任何超过 100 毫秒的超时也有效。在这一点上,它看起来很像比赛条件?
export function initializeApp1(afa: AngularFireAuth): any {
return () => {
return new Promise(resolve => {
afa.useEmulator(`http://${location.hostname}:9099/`);
setTimeout(() => resolve(), 100); // delay Angular initialization by 100ms
});
};
}
@NgModule({
declarations: [
...
],
imports: [
...,
AngularFireModule.initializeApp(environment.firebase),
AngularFireAuthModule,
...
],
providers: [
// Provide the AngularFireAuth constructor with the emulator parameters
{
provide: USE_EMULATOR,
useValue: [location.hostname, 9099]
},
// Delay the app initialization process by 100ms
{
provide: APP_INITIALIZER,
useFactory: initializeApp1,
// for some reason this dependency is necessary for this solution to work.
// Maybe in order to trigger the constructor *before* waiting 100ms?
deps: [AngularFireAuth],
multi: true
}
],
bootstrap: [AppComponent]
})
我还注意到
this comment在
AngularFireAuth
源代码。到目前为止,我真的不明白发生了什么,但也许问题是相关的:
// HACK, as we're exporting auth.Auth, rather than auth, developers importing firebase.auth (e.g,
import { auth } from 'firebase/app'
) are getting an undefined auth object unexpectedly as we're completely lazy. Let's eagerly load the Auth SDK here. There could potentially be race conditions still... but this greatly decreases the odds while we reevaluate the API.
最佳答案
我已经在我的代码中调整了初始化应用程序函数,以使用 rxjs 而不是 Promise。
export const initializeApp = (angularFireAuth: AngularFireAuth): (() => Observable<boolean>) => {
if (!environment.firebaseConfig.useEmulator) {
return () => of(true);
} else {
return () => of(true).pipe(
delay(100),
tap(_ => {
angularFireAuth.useEmulator('http://localhost:9099/');
})
);
}
};
关于firebase - AngularFireAuth 模拟器登录在页面重新加载时丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65025005/
我想要一个包装 AngularFireAuth 服务的 AuthWrapper 服务。像这样。 // EDIT: Adding some import statements. import {Test
这个问题在这里已经有了答案: Firebase kicks out current user (19 个回答) 关闭 3 年前。 我使用带有 firebase 的 Angular 应用程序,并且我具
是否可以在 Firebase 中使用附加字段创建用户?在我的代码中,我可以使用“电子邮件”和“密码”创建它们。 this.af.auth.createUser({ name: formDa
我的 Angular 项目成功使用了 AngularFireAuth可从 @angular/fire 注入(inject)连接到本地 Firebase 身份验证模拟器。我使用这些资源作为我的实现的引用
我已更改官方 AngularJS 文档以使用 Firebase(手机教程)。 这是我的应用程序的路由器: angular.module('phonecat', ['firebase']). conf
我正在使用 Ionic4 和 @angular/fire 编写聊天应用程序代码,但在运行我的应用程序时出现白页和错误消息 (F12)。有人可以帮助我吗? 谢谢。 应用程序模块.ts import {
如何获取登录用户? 我正在这样做: console.log('正在登录...'); this.afAuth.auth.signInWithEmailAndPassword(this.email, th
亲爱的 stackoverflow 社区, 我有以下问题。我尝试将 FirebaseAuthentication 与 Angular7 一起使用,并尝试使用守卫保护路由,因此只有登录用户才能访问 /p
我有一个核心服务,它获取 firebase.auth obj 的实例并使用 getter 返回它。它很简单,如下所示: export class AuthService { ... const
我已经使用 AngularFire2 使用 jasmine/karma 为我的 Angular2 Web 应用程序创建了测试用例,但遇到了一个奇怪的错误,我无法调试。当我运行测试(检查应用程序组件是否
我有一个核心服务,它获取 firebase.auth obj 的实例并使用 getter 返回它。它很简单,如下所示: export class AuthService { ... const
我有一个 authService实例化时订阅 AngularFireAuth的可观察 authState并设置服务的内部(私有(private))属性authState . 所以我可以单元测试auth
有人知道 Angularfire 的新代码 - Angular 4 signInWithEmailAndPassword 方法吗? loginWithEmail(email: string, pass
我已经使用 Firebase 身份验证和 Cloud Fire Store 建立了一个新的 Angular 6 项目。有一个登录页面,您可以在其中通过谷歌登录,用户数据保存在 Firestore 中(
我正在使用 Angular 和 firebase(使用 AngularFire2)构建一个应用程序。尝试在任何 firebase 可观察对象(FirebaseAuthObservable、Fireba
抱歉,我想不出更好的方法来包含所有信息...当我运行它时,我收到一条错误消息,内容如下。我已经按照 Ionic Docs 到 T,我无法弄清楚可能出了什么问题。 错误: No provider for
我知道以前曾有人问过这个问题,但我见过的解决方案都不适合我。我使用 firebase-ui 为我的 ionic 应用程序构建一个登录页面,我使用与其他应用程序完全相同的方法,并且它在那里工作。我安装了
我们尝试使用(Firebase/ionic2/angularjs2)通过谷歌身份验证登录。我们的代码 import { Component } from '@angular/core'; impor
我正在尝试将 angularFireAuth(来自 AngularFire - Firebase)与 ui-route 结合使用。 问题是 angularFireAuth 需要 ng-route 作为
我一直在兜圈子,试图对依赖于 AngularFireAuth 的服务 (AuthService) 进行单元测试。 我正在尝试找到一种方法来模拟或劫持 Observable AngularFireAut
我是一名优秀的程序员,十分优秀!