简体   繁体   English

Cordova Firebase 谷歌身份验证

[英]Cordova firebase google auth

I'm trying to implement Google Auth in a cordova (phonegap) app.我正在尝试在cordova(phonegap)应用程序中实现Google Auth。

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

  • I have created an auth domain in firebase xxxx.firebaseapp.com我在 firebase xxxx.firebaseapp.com 中创建了一个身份验证域
  • My app is agregated to firebase (I'm was using firebase for analytics) in the same app.我的应用程序在同一个应用程序中聚合到 firebase(我正在使用 firebase 进行分析)。
  • I have enabled Firebase Dynamic Links, and I can view my domain xxxx.page.link我已启用 Firebase 动态链接,并且可以查看我的域 xxxx.page.link
  • I have enabled google sign method.我已启用谷歌签名方法。
  • I have installed all required plugins我已经安装了所有必需的插件
  • In my config.xml:在我的 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" />

My login method:我的登录方式:

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!!!!

            });
});

When I execute my login method, a new chrome tab is launched, and I put my google user and password.当我执行登录方法时,会启动一个新的 chrome 选项卡,然后输入我的 google 用户名和密码。 Then, tab is closed and app is showed again, but I allways get an error in my console: "error1 the redirect operation has been cancelled by the user before finalizing."然后,选项卡关闭并再次显示应用程序,但我总是在控制台中收到错误消息:“error1 重定向操作已被用户在最终确定之前取消。”

the first time I got this error I put this code when the app loads:我第一次收到此错误时,我在应用加载时放置了此代码:

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);
    });

I've tried catch all Dinamic links events,to test, but no event is recieved:我尝试捕获所有 Dinam 链接事件进行测试,但没有收到任何事件:

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

but it never run :(但它永远不会运行:(

Can anyone help me please.任何人都可以请帮助我。 I'm exausted whith this situation.我对这种情况感到筋疲力尽。

I'm going to post an answer to my own question, because it can be usefull for other people.我将发布我自己问题的答案,因为它对其他人有用。

First time, if yoy follow the guide https://firebase.google.com/docs/auth/web/cordova?hl=en-419 , it works.第一次,如果 yoy 遵循指南https://firebase.google.com/docs/auth/web/cordova?hl=en-419 ,它就可以工作。 But...但...

Cordova universal link plugin has an issue whith newest cordova versions, not solved yet: https://github.com/nordnet/cordova-universal-links-plugin/issues/133 Cordova 通用链接插件有最新的cordova 版本问题,尚未解决: https : //github.com/nordnet/cordova-universal-links-plugin/issues/133

You can fix the plugin manually: In this file: ./plugins/cordova-universal-links-plugin/hooks/lib/android/manifestWriter.js change at line 21:您可以手动修复插件:在此文件中:./plugins/cordova-universal-links-plugin/hooks/lib/android/manifestWriter.js 在第 21 行更改:

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

to:到:

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

And, if you are having problems with universal links, take a look to the manifest resulting file: 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>

And check if this configuration it's equivalentu whith your config.xml.并检查此配置是否与您的 config.xml 相同。 You must not change Manifest file!!!您不得更改 Manifest 文件!!! It's only for check it.这只是为了检查它。 If not, check if you have more than one annotation about universal links in the config.xml.如果没有,请检查在 config.xml 中是否有多个关于通用链接的注释。

Was anyone successfully able to authenticate users after the redirect?重定向后是否有人成功验证用户身份? I get nothing back from the DL Event Listeners and a Firebbase Error: The redirect operation has been cancelled by the user before finalizing.我没有从 DL 事件侦听器和 Firebbase 错误中得到任何回报: The redirect operation has been cancelled by the user before finalizing.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM