【问题标题】:Cordova firebase google auth科尔多瓦火力基地谷歌身份验证
【发布时间】:2019-01-09 06:32:21
【问题描述】:

我正在尝试在 cordova (phonegap) 应用中实现 Google Auth。

https://firebase.google.com/docs/auth/web/cordova?hl=en-419

  • 我在 firebase xxxx.firebaseapp.com 中创建了一个身份验证域
  • 我的应用在同一个应用中聚合到 firebase(我正在使用 firebase 进行分析)。
  • 我已启用 Firebase 动态链接,并且可以查看我的域 xxxx.page.link
  • 我已启用谷歌签名方法。
  • 我已经安装了所有必需的插件
  • 在我的 config.xml 中:

    <universal-links>
    <host name="xxxx.page.link" scheme="https" />
    <host name="xxxx.firebaseapp.com" scheme="https">
    <path url="/__/auth/callback"/>
    </host>
    </universal-links>
    <preference name="AndroidLaunchMode" value="singleTask" />
    

我的登录方式:

var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithRedirect(provider).then(function() {
                console.log("hola");
                return firebase.auth().getRedirectResult();
}).then(function(result) {
                // This gives you a Google Access Token.
                // You can use it to access the Google API.
                var token = result.credential.accessToken;
                // The signed-in user info.
                var user = result.user;
                afterLoginGoogle(user);
}).catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
                console.log("error1 "+ errorMessage);   //Error log!!!!

            });
});

当我执行登录方法时,会启动一个新的 chrome 选项卡,并输入我的 google 用户名和密码。然后,关闭选项卡并再次显示应用程序,但我的控制台中总是出现错误: "error1 重定向操作在完成之前已被用户取消。"

我第一次收到此错误时,我在应用加载时输入了此代码:

firebase.auth().getRedirectResult().then(function(result) {
        if (result.credential) {
        // This gives you a Google Access Token.
        // You can use it to access the Google API.
        var token = result.credential.accessToken;
        // The signed-in user info.
        var user = result.user;
        // ...
        console.log("Despues");
        afterLoginGoogle(user);
        }
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log("Despues"+errorMessage);
    });

我已尝试捕获所有动态链接事件进行测试,但未收到任何事件:

universalLinks.subscribe(null, function(eventData) {
            alert('Did launch application from the link: ' + eventData.url);
});

但它永远不会运行:(

谁能帮帮我。我对这种情况感到筋疲力尽。

【问题讨论】:

    标签: android cordova firebase oauth


    【解决方案1】:

    我将发布我自己问题的答案,因为它对其他人有用。

    第一次,如果你按照指南 https://firebase.google.com/docs/auth/web/cordova?hl=en-419 ,它的工作原理。但是……

    Cordova 通用链接插件在最新的 Cordova 版本中存在问题,尚未解决:https://github.com/nordnet/cordova-universal-links-plugin/issues/133

    您可以手动修复插件: 在这个文件中:./plugins/cordova-universal-links-plugin/hooks/lib/android/manifestWriter.js 在第 21 行更改:

    var pathToManifest = path.join(cordovaContext.opts.projectRoot, 'platforms', 'android', 'AndroidManifest.xml');
    

    到:

    var pathToManifest = path.join(
        cordovaContext.opts.projectRoot,
        'platforms',
        'android',
        'app',
        'src',
        'main',
        'AndroidManifest.xml');
    

    如果您在使用通用链接时遇到问题,请查看清单生成文件:platforms/android/app/src/main/AndroidManifest.xml

      <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="XXXXXXX.page.link" android:scheme="https"/>
      </intent-filter>
      <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="XXXXXXXXX.firebaseapp.com" android:scheme="https" android:path="/__/auth/callback"/>
      </intent-filter>
    

    并检查此配置是否与您的 config.xml 相同。您不得更改 Manifest 文件!!!这只是为了检查它。如果没有,请检查 config.xml 中是否有多个关于通用链接的注释。

    【讨论】:

    • 我遇到了同样的错误,我正在尝试您推荐的方法,但收到此错误错误:无法读取未定义的属性“清单”
    • 尝试执行“phonegap clean”或“cordova clean”
    【解决方案2】:

    重定向后是否有人成功验证用户身份?我没有从 DL 事件侦听器和 Firebbase 错误中得到任何回报:The redirect operation has been cancelled by the user before finalizing.

    【讨论】:

    • 如果您收到该错误,可能是您的通用链接配置有问题。我花了几个小时试图找出问题所在,但最后我在 config.xml 文件中出现了配置错误。
    • 发现配置中的通用链接设置有误。我的回调地址错了哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2015-01-25
    • 2016-11-21
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多