【问题标题】:Firebase Authentication API Email/Password AndroidFirebase 身份验证 API 电子邮件/密码 Android
【发布时间】:2016-10-13 03:41:42
【问题描述】:

我正在尝试编写一个通过电子邮件/密码使用 Firebase 身份验证的 Android 应用。它已启用。然而,教程和 Github 中的示例代码显示:

私有 FirebaseAuth mAuth;

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.firebase:firebase-core:9.0.2'



}


apply plugin: 'com.google.gms.google-services'

但是,我收到一个错误,好像“FirebaseAuth”不存在。但是,最新的文档另有说明。

Github sample code

任何帮助将不胜感激。

【问题讨论】:

    标签: android firebase firebase-authentication


    【解决方案1】:

    com.google.firebase:firebase-core:9.0.2' 依赖替换为com.google.firebase:firebase-auth:9.0.2 依赖。所以:

    compile 'com.google.firebase:firebase-auth:9.0.2'

    而不是

    compile 'com.google.firebase:firebase-core:9.0.2' 在您的依赖项下。

    我没有在 core 依赖项中找到 FirebaseAuth 类,但我确实在 auth 依赖项中找到了它。此外,如果您检查 their 依赖项列表,他们不会添加 core 依赖项,而是添加 auth 依赖项。

    【讨论】:

    • 它们不都一样吗?我对依赖关系有点困惑。使用 Firebase SDK 3.0
    • 不,它们不一样。依赖项只是供您使用的外部库,仅此而已。至于为什么他们有不止一个,如果他们将所有不同的类型打包在一个中,那将是巨大的。衡量依赖项足迹的一种方法是通过方法计数,方法计数越高,应用的启动成本和 APK 大小就越高。所以它们是分开的。
    • 哇。我很抱歉。我错过了身份验证/核心差异。谢谢你。我会尝试,这很可能会解决我的问题。我会回来的,如果可行,我会接受你的回答。
    • 还值得澄清的是,由库的维护者决定如何拆分它。如果我没记错的话,有一些库可以选择将整个库编译为依赖项或只是其中的一部分。我没有广泛使用 Firebase,但似乎没有重叠,或者如果有重叠,在这种情况下它不能满足您的需求。
    • 这很有效,非常感谢您的建设性反馈。这确实帮助我更好地理解了依赖关系。
    【解决方案2】:

    根据 firebase 网页中的文档,您应该使用您的 firebase 中的 URL 创建一个 Firebase 对象,并从那里创建带有密码的用户名或登录。您展示的代码为此使用了这个 FirebaseAuth。

    这是创建新用户的代码:

    Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
    ref.createUser("bobtony@firebase.com", "correcthorsebatterystaple", new Firebase.ValueResultHandler<Map<String, Object>>() {
        @Override
        public void onSuccess(Map<String, Object> result) {
            System.out.println("Successfully created user account with uid: " + result.get("uid"));
        }
        @Override
        public void onError(FirebaseError firebaseError) {
            // there was an error
        }
    });
    

    这是他登录的代码:

    Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
    ref.authWithPassword("bobtony@firebase.com", "correcthorsebatterystaple", new Firebase.AuthResultHandler() {
        @Override
        public void onAuthenticated(AuthData authData) {
            System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
        }
        @Override
        public void onAuthenticationError(FirebaseError firebaseError) {
            // there was an error
        }
    });
    

    从此处的快速入门指南中获取所有这些信息:https://www.firebase.com/docs/android/guide/login/password.html#section-logging-in

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2019-07-13
      • 1970-01-01
      • 2018-11-11
      • 2013-07-11
      • 2021-11-21
      • 2019-08-05
      • 2019-03-15
      • 2016-11-13
      • 1970-01-01
      相关资源
      最近更新 更多