具有PhoneGap + Firebase身份验证问题的混合应用

时间:2017-03-24 09:51:50

标签: javascript html5 cordova firebase google-authentication

我正在创建一个目前有工作的应用程序,我们已选择使用Cordova / Phonegap的混合应用程序,因此我们可以为Windows / android / IOS提供一个源代码。

我们遇到问题的地方是身份验证。在浏览了网上并关注firebase文档中的教程和示例代码之后,我就不再接受这项工作了。

我们将使用google Auth开始。

由于最近的一些变化,我通过基于Web视图的应用程序使用身份验证不再适用。并且使用InAppBrowser打开googleAuth弹出窗口或重定向的完整示例的方式并不多。

以下代码理想情况下我们想要使用的代码:(根据Firebase的示例编辑) 问题是这在Cordova / PhoneGap应用程序中不起作用,但在浏览器中的PC / Mac上完美运行

有人可以解释我如何编辑它以在应用程序中工作吗?

<!-- Material Design Theming -->
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.orange-indigo.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>

<link rel="stylesheet" href="main.css">
<script src="https://www.gstatic.com/firebasejs/3.7.3/firebase.js"></script>
<script>
    // Initialize Firebase
    var config = {
        apiKey: "xxx",
        authDomain: "xxx.firebaseapp.com",
        databaseURL: "https://xxx.firebaseio.com",
        storageBucket: "xxx.appspot.com",
        messagingSenderId: "xxx"
    };
    firebase.initializeApp(config);
</script>
<script type="text/javascript">

    /**
     * Function called when clicking the Login/Logout button.
     */
    // [START buttoncallback]
    function toggleSignIn() {
        if (!firebase.auth().currentUser) {
            // [START createprovider]
            var provider = new firebase.auth.GoogleAuthProvider();
            // [END createprovider]
            // [START addscopes]
            provider.addScope('https://www.googleapis.com/auth/plus.login');
            // [END addscopes]
            // [START signin]
            firebase.auth().signInWithPopup(provider).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;
                // [START_EXCLUDE]
                document.getElementById('quickstart-oauthtoken').textContent = token;
                // [END_EXCLUDE]
            }).catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
                // The email of the user's account used.
                var email = error.email;
                // The firebase.auth.AuthCredential type that was used.
                var credential = error.credential;
                // [START_EXCLUDE]
                if (errorCode === 'auth/account-exists-with-different-credential') {
                    alert('You have already signed up with a different auth provider for that email.');
                    // If you are using multiple auth providers on your app you should handle linking
                    // the user's accounts here.
                } else {
                    console.error(error);
                }
                // [END_EXCLUDE]
            });
            // [END signin]
        } else {
            // [START signout]
            firebase.auth().signOut();
            // [END signout]
        }
        // [START_EXCLUDE]
        document.getElementById('quickstart-sign-in').disabled = true;
        // [END_EXCLUDE]
    }
    // [END buttoncallback]

    /**
     * initApp handles setting up UI event listeners and registering Firebase auth listeners:
     *  - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or
     *    out, and that is where we update the UI.
     */
    function initApp() {
        // Listening for auth state changes.
        // [START authstatelistener]
        firebase.auth().onAuthStateChanged(function(user) {
            if (user) {
                // User is signed in.
                var displayName = user.displayName;
                var email = user.email;
                var emailVerified = user.emailVerified;
                var photoURL = user.photoURL;
                var isAnonymous = user.isAnonymous;
                var uid = user.uid;
                var providerData = user.providerData;
                // [START_EXCLUDE]
                document.getElementById('quickstart-sign-in-status').textContent = 'Signed in';
                document.getElementById('quickstart-sign-in').textContent = 'Sign out';
                document.getElementById('quickstart-account-details').textContent = JSON.stringify(user, null, '  ');
                // [END_EXCLUDE]
            } else {
                // User is signed out.
                // [START_EXCLUDE]
                document.getElementById('quickstart-sign-in-status').textContent = 'Signed out';
                document.getElementById('quickstart-sign-in').textContent = 'Sign in with Google';
                document.getElementById('quickstart-account-details').textContent = 'null';
                document.getElementById('quickstart-oauthtoken').textContent = 'null';
                // [END_EXCLUDE]
            }
            // [START_EXCLUDE]
            document.getElementById('quickstart-sign-in').disabled = false;
            // [END_EXCLUDE]
        });
        // [END authstatelistener]

        document.getElementById('quickstart-sign-in').addEventListener('click', toggleSignIn, false);
    }

    window.onload = function() {
        initApp();
    };
</script>

   

                   <button disabled class="mdl-button mdl-js-button mdl-button--raised" id="quickstart-sign-in">Sign in with Google</button>

                <!-- Container where we'll display the user details-->
                <div class="quickstart-user-details-container">
                    Firebase sign-in status: <span id="quickstart-sign-in-status">Unknown</span>
                    <div>Firebase auth <code>currentUser</code> object value:</div>
                    <pre><code id="quickstart-account-details">null</code></pre>
                    <div>Google OAuth Access Token:</div>
                    <pre><code id="quickstart-oauthtoken">null</code></pre>
                </div>
<style>
    div.quickstart-user-details-container 
        display: none;
    }

</style>

1 个答案:

答案 0 :(得分:1)

AskFirebase

Firebase假设您只会使用&#34; http://&#34;或&#34; https://&#34;而不是像#34; local://&#34;或&#34; file://&#34;因此,您无法将其添加到“授权域名”中。部分。如果AskFirebase人员考虑混合开发路径 - 提示,那将是很好的。 logging-cookbook有一些信息 但这并不明显,例如在“授权域名”附近的任何地方。配置区域或任何示例附近。 #AskFirebase