首页 文章

Ionic 2 App中的Firebase Auth网络错误

提问于
浏览
6

我的Firebase / Ionic 2应用程序在浏览器(离子服务)和本地Android设备(离子运行android)上运行良好 . 该应用使用Firebase数据库和身份验证功能 .

该应用程序也适用于iOS设备 .

但是,当我在Android上发布到Google Play进行Beta版测试时,auth登录方法始终失败并显示错误:“发生了网络错误(例如超时,中断连接或无法访问的主机) . ”但Firebase数据库方法运行正常 . 我只使用Firebase电子邮件/密码提供商 .

我已经阅读了我能找到的与此问题相似的所有帖子,并尝试了所有这些解决方案 . 我已升级到所有组件的最新版本 .

已安装cordova-plugin-whitelist插件 . 它默认安装在一个新的Ionic 2项目中 . 我的理解是以下安全设置没有阻止Firebase .

index.html

<meta http-equiv="Content-Security-Policy" content="font-src * data:; img-src * data:; default-src * 'unsafe-eval' 'unsafe-inline'; script-src * 'unsafe-eval' 'unsafe-inline'">

config.xml

<access origin="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<allow-navigation href="*"/>

My Service

public login(email: string, password: string): Observable<any> {
    return Observable.fromPromise(
        <Promise<any>>firebase.auth().signInWithEmailAndPassword(email, password)
    );
}

My Form

this.authService.login(this.loginForm.value.email, this.loginForm.value.password)
        .subscribe(() => {
            // Do something!
        }, error => {
            // A network error has occurred!
        });

Version Info

Cordova CLI: 6.5.0
Ionic Framework Version: 2.2.0
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.4
Node Version: v7.2.1

In package.json: "firebase": "^3.7.1"
In config.xml: <plugin name="cordova-plugin-whitelist" spec="1.3.1"/>

1 回答

  • 2

    我能够通过在本地构建并在下面指定安全配置来解决此问题 .

    请注意,我无法找到适合 生产环境 的配置 . 我怀疑我在白名单中遗漏了一个域名 .

    我一直在使用ionic.io生成.apk,并且由于某种原因它产生了有问题的构建 . 除了Firebase身份验证之外,一切都很有用 .

    index.html

    <meta http-equiv="Content-Security-Policy" content="default-src * data: gap://ready https://ssl.gstatic.com file://* ws://* wss://*; img-src * data:; font-src * data:; script-src * https://*.google-analytics.com https://*.googleapis.com https://*.firebaseio.com 'unsafe-eval' 'unsafe-inline';">
    

    注意使用 default-src *script-src * . 将其中任何一个更改为'self'都会导致错误 .

    config.xml

    <access origin="*"/>
      <access origin="https://*.google-analytics.com"/>
      <access origin="https://*.googleapis.com"/>
      <access origin="https://*.firebaseio.com"/>
      <allow-navigation href="*"/>
      <allow-navigation href="http://ionic.local/*"/>
      <allow-intent href="http://*/*"/>
      <allow-intent href="https://*/*"/>
      <allow-intent href="tel:*"/>
      <allow-intent href="sms:*"/>
      <allow-intent href="mailto:*"/>
      <allow-intent href="geo:*"/>
    

    注意 <access origin="*"/> 的使用 . 删除此行会导致错误 .

    有趣的是,删除 <access origin="*"/> 行并在本地构建会导致与使用ionic.io构建时相同的错误 .

相关问题