GVKun编程网logo

工厂:获取Firebase简单登录的当前user.id(电子邮件/密码)

9

在这篇文章中,我们将为您详细介绍工厂:获取Firebase简单登录的当前user.id的内容,并且讨论关于电子邮件/密码的相关问题。此外,我们还会涉及一些关于AndroidStudio的Firebas

在这篇文章中,我们将为您详细介绍工厂:获取Firebase简单登录的当前user.id的内容,并且讨论关于电子邮件/密码的相关问题。此外,我们还会涉及一些关于Android Studio 的 Firebase 工具中缺少电子邮件和密码身份验证、android – Firebase Auth – 带电子邮件和密码 – 检查用户是否已注册、Android – Firebase快速入门电子邮件/密码验证演示不起作用、Android – Firebase身份验证无法使用电子邮件/密码设置的知识,以帮助您更全面地了解这个主题。

本文目录一览:

工厂:获取Firebase简单登录的当前user.id(电子邮件/密码)

工厂:获取Firebase简单登录的当前user.id(电子邮件/密码)

我正在寻找一种可靠的方式来在所有可用控制器中使用“当前用户ID”。使用:Firebase简单登录:电子邮件/密码身份验证

我的ida:我需要一个“工厂”,我可以将其注入控制器中,以使“当前用户ID”始终可用。

我想出了以下代码:

    app.factory(''User'', [''angularFire'',    //Get Current UserID    function(angularFire){        console.log (''FACTORY: User'');            var currentUser = {};            var ReturnStr = '''';        var ref = new Firebase("https://myFIREBASE.firebaseio.com/");        var authClient = new FirebaseAuthClient(ref, function (err, user) {            if (err) {                    ReturnStr = ''FACTORY: User Error: '' + err;                 console.log (ReturnStr);                        //var User = ReturnStr;            } else if (user) {                console.log (''FACTORY: User: Login successfully:'');                console.log (user);                currentUser = user;            } else {                //console.log (''-----------User: Logged Out ---------------'');                    ReturnStr = ''FACTORY: Logged out: Redirect to Login'';                 console.log (ReturnStr);                window.location.href = "/login.php";            }        });    return currentUser;        }]);

我最简单的Controller看起来像:

function ToDoCtrl($scope, User) {    $scope.MyUser = User;    $scope.MyUser.test = ''Test'';}

在HTML(角部分)中,我有:

<h2>{{MyUser.id}}</h2><h2>{{MyUser.email}}</h2><h2>{{MyUser.provider}}</h2><h2>{{MyUser.test}}</h2>

=> ID,电子邮件,提供商未定义。在控制台中,我看到带有正确用户-对象的“工厂:用户:成功登录:”。

=>数据异步加载有问题吗?

我也尝试过(没有运气):

        $timeout(function () {        currentUser = user;        }

这样的工厂将非常有用!感谢您指出正确的方向!

编辑1.1: 现在,通过$ rootscope hack

=>效果相同-mycontroller太快-出厂速度慢。

app.factory(''User'', [''$rootScope'', ''$timeout'', ''angularFire'',    //Aktueller Benutzer auslesen    function($rootScope, $timeout, angularFire){        console.log (''FACTORY: User'');            var currentUser = {};            var ReturnStr = '''';        var ref = new Firebase("https://openpsychotherapy.firebaseio.com/");        var authClient = new FirebaseAuthClient(ref, function (err, user) {            if (err) {                    ReturnStr = ''FACTORY: User Error: '' + err;                 console.log (ReturnStr);                        //var User = ReturnStr;            } else if (user) {                console.log (''FACTORY: User: Login successfully:'');                        //currentUser = user;            $timeout(function () {                        ReturnStr = ''FACTORY: Inside timout'';                   console.log (ReturnStr);                            currentUser = user;                  console.log (currentUser);                $rootScope.myUser = user;                $rootScope.myUserID = user.id;                $rootScope.loggedIn = true;                            $rootScope.$apply();                  return currentUser;            });            } else {                //console.log (''-----------User: Logged Out ---------------'');                    ReturnStr = ''FACTORY: Logged out: Redirect to Login'';                 console.log (ReturnStr);                        //var User = ReturnStr;                window.location.href = "/login.php";            }        });    return currentUser;        }]);

TAHNKS提供任何有用的建议! 不知道其他人如何解决这个问题!

答案1

小编典典

所以这是我解决这个确切问题的方法。我正在为我的Angular应用程序使用Firebase,FirebaseAuthClient和angularFire。我在登录系统时遇到了同样的情况,您无法将$
scope注入工厂,因此我想出了一个控制器,该控制器使用工厂作为其方法来检索,添加,更新和删除东西。在控制器中,我正在运行firebaseAuth东西,User值的设置以及我为其分配的引用。一旦用户登录,他们将被重定向到另一个位置,此时,在该地址位置,app.js文件将由子控制器接管。

我的登录名也使用localStorage,因此登录名将持续存在,您可以刷新而不必继续登录,并且可以将其更改为cookie或sessionStorage足够容易。

特别是如果您选择使用此方法,这将需要适应您的需求,无论如何它都非常复杂,但这对我来说非常可靠,现在我不再需要担心firebaseAuth或angularFire的问题我的工厂都已设置为可以来回传递数据。我只是用指令来做angularJS东西。所以这是我的代码。

注意: 这将需要进行修改,并且某些事情将是伪的或开放式的,以便您确定需要。

AuthCtrl.js

''use strict'';angular.module(''YOUR_APP'', []).controller(''AuthCtrl'', [''$scope'',''$location'',''angularFire'',''fireFactory'',function AuthCtrl($scope, $location, angularFire, fireFactory) {    // FirebaseAuth callback    $scope.authCallback = function(error, user) {        if (error) {            console.log(''error: '', error.code);            /*if (error.code === ''EXPIRED_TOKEN'') {                $location.path(''/'');            }*/        } else if (user) {            console.log(''Logged In'', $scope);            // Store the auth token            localStorage.setItem(''token'', user.firebaseAuthToken);            $scope.isLoggedIn = true;            $scope.userId = user.id;            // Set the userRef and add user child refs once            $scope.userRef = fireFactory.firebaseRef(''users'').child(user.id);            $scope.userRef.once(''value'', function(data) {                // Set the userRef children if this is first login                var val = data.val();                var info = {                    userId: user.id,                    name: user.name                };                // Use snapshot value if not first login                if (val) {                    info = val;                }                $scope.userRef.set(info); // set user child data once            });            $location.path(''/user/'' + $scope.userRef.name());        } else {            localStorage.clear();            $scope.isLoggedIn = false;            $location.path(''/'');        }    };    var authClient = new FirebaseAuthClient(fireFactory.firebaseRef(''users''), $scope.authCallback);    $scope.login = function(provider) {        $scope.token = localStorage.getItem(''token'');        var options = {            ''rememberMe'': true        };        provider = ''twitter'';        if ($scope.token) {            console.log(''login with token'', $scope.token);            fireFactory.firebaseRef(''users'').auth($scope.token, $scope.authCallback);        } else {            console.log(''login with authClient'');            authClient.login(provider, options);        }    };    $scope.logout = function() {        localStorage.clear();        authClient.logout();        $location.path(''/'');    };}

]);

现在,这是一个漂亮,简单但可重复使用的工厂。您需要为您的应用设置Firebase路径,以使baseUrl变量起作用。

fireFactory.js

''use strict'';angular.module(''YOUR_APP'').factory(''fireFactory'', [function fireFactory() {    return {        firebaseRef: function(path) {            var baseUrl = ''https://YOUR_FIREBASE_PATH.firebaseio.com'';            path = (path !== '''') ?  baseUrl + ''/'' + path : baseUrl;            return new Firebase(path);        }    };}

]);

信息

您只给工厂提供了一部分路径引用,例如“用户”,它将用作要存储用户数据的完整路径引用的一部分。

fireFactory.firebaseRef(''users'')

为用户设置参考集后,无需再次设置用户参考,它只会使用现有数据和.auth()。另外,如果localStorage中存在现有的“令牌”,它将也使用该令牌来认证用户。

否则,它将使用用户提供的任何选项登录()用户并弹出Oauth窗口,以便他们这样做。

在Firebase /
FirebaseAuthClient和angularFire方面,我花了很多时间,很多小时甚至几天甚至更长的时间来寻找比这更好的东西。通过Firebase
API和FireAuth
API的方式,当无论如何将它们与angularFire一起使用时,使它们彼此很好地玩耍是很烦人的。这非常令人沮丧,但我终于克服了它。

如果您想查看我的应用程序的代码并查看我如何更完整地执行这些操作,可以在我的Webernote github
repo的此分支中找到它。

随意分叉,在本地安装和运行它,或者即使您愿意也可以对其做出贡献。我可以自己帮忙:)

希望这对您有帮助!!

Android Studio 的 Firebase 工具中缺少电子邮件和密码身份验证

Android Studio 的 Firebase 工具中缺少电子邮件和密码身份验证

如何解决Android Studio 的 Firebase 工具中缺少电子邮件和密码身份验证?

enter image description here

从上面的照片中可以看出,我没有看到电子邮件和密码身份验证选项。我的 Firebase 工具中缺少它。我正在尝试创建一个登录和注销活动示例应用程序,我想使用电子邮件和密码身份验证选项,但它根本不存在。有谁知道在哪里可以找到/启用它?

解决方法

这些只是快捷方式,并未显示所有身份验证选项 只需将您的项目与 firebase 连接并从此工具栏添加依赖项,然后从浏览器的 firebase 控制台启用电子邮件身份验证。

,

您必须转到 Firebase 控制台并按照我在下一个屏幕截图中标记的步骤进行操作

点击身份验证,然后点击登录方法

然后在电子邮件/密码行上,您必须用铅笔单击图标并激活我在下一张照片上标记的开关

enter image description here

android – Firebase Auth – 带电子邮件和密码 – 检查用户是否已注册

android – Firebase Auth – 带电子邮件和密码 – 检查用户是否已注册

我想检查用户是否尝试使用Firebase用户身份验证方法中的createuserWithEmailAndPassword()进行注册,此用户已在我的应用中注册.

解决方法:

要检测具有该电子邮件地址的用户是否已存在,您可以检测对createuserWithEmailAndPassword()的调用何时因auth / email-in-use而失败.我看到@Srinivasan刚刚发布了一个答案.

或者,您可以通过调用fetchSignInMethodsForEmail()(之前称为fetchProvidersForEmail())来检测已使用的电子邮件地址.

Android – Firebase快速入门电子邮件/密码验证演示不起作用

Android – Firebase快速入门电子邮件/密码验证演示不起作用

所以,这里有几个类似的问题,但它们都没有为我提供有效的解决方案.

我正在使用从Github下载的Quickstart演示.今天下载了.我在演示代码中没有改变任何内容.

我已经解决过的一些常见错误(来自我所读到的):

>我在Firebase控制台中创建了项目
可下载的json文件(使用包名创建).我提供了
我的SHA1指纹.
>选中电子邮件/密码登录方法.
>我已经更新并安装了Google Play服务和Google Repository(来自Android
Studio SDK经理)
>在我的设备上,Google Play服务版本为9.4.52

如果我打开Firebase控制台中的匿名登录选项,我就可以成功匿名登录演示应用.但电子邮件/密码不起作用.

编辑:不确定我是否正确,但我在Logcat中得到了这些

D/EmailPassword: createAccount:chris884@gmail.com
 W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
 W/ResourcesManager: Asset path ''/system/framework/com.android.media.remotedisplay.jar'' does not exist or contains no resources.
 W/ResourcesManager: Asset path ''/system/framework/com.android.location.provider.jar'' does not exist or contains no resources.
 D/EmailPassword: createuserWithEmail:onComplete:false

解决方法

logcat输出显示用户帐户的创建失败. documentation表明这可能是由于以下原因:

>密码不够强大(少于6个字符)
>电子邮件地址格式错误
>已存在具有给定电子邮件地址的帐户

将Log语句添加到createuserWithEmailAndPassword()的完成侦听器,以查看失败原因:

@Override
public void onComplete(@NonNull Task<AuthResult> task) {
    Log.d(TAG,"createuserWithEmail:onComplete:" + task.isSuccessful());

    // If sign in fails,display a message to the user. If sign in succeeds
    // the auth state listener will be notified and logic to handle the
    // signed in user can be handled in the listener.
    if (!task.isSuccessful()) {
        Log.d(TAG,"onComplete: Failed=" + task.getException().getMessage()); //ADD THIS

        Toast.makeText(EmailPasswordActivity.this,R.string.auth_Failed,Toast.LENGTH_SHORT).show();
    }

    // [START_EXCLUDE]
    hideProgressDialog();
    // [END_EXCLUDE]
}

Android – Firebase身份验证无法使用电子邮件/密码设置

Android – Firebase身份验证无法使用电子邮件/密码设置

所以,我有我的Firebase项目设置,以及build.gradle中应该需要的所有插件(我选中了五元组).但是,当我运行我的应用程序并尝试运行以下方法时:

createuserWithEmailAndPassword(email, password)

它不起作用,我不断得到一些东西 – 我将在下面显示 – 在日志中,并且通知管理器弹出“未找到频道”.我没有使用通知管理器,但我相信它是在Android Runtime环境中完成的.

在用户在GUI中键入用户名和密码后调用此方法,并使用上述方法尝试与我的firebase项目创建连接.

1-12 01:36:17.181 6593-6593/com.example.Sarah.whosthere I/BiChannelGoogleApi: [FirebaseAuth: ] No Fallback module; NOT setting up for lazy initialization 
11-12 01:37:50.967 6593-6593/com.example.Sarah.whosthere W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms
11-12 01:37:51.140 1681-3474/system_process E/NotificationService: No Channel found for pkg=com.example.Sarah.whosthere, channelId=null, id=10436, tag=null, opPkg=com.example.Sarah.whosthere, callingUid=10085, userId=0, incomingUserId=0, notificationUid=10085, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x110 color=0x00000000 vis=PRIVATE)

此外,在调试模式下,在此方法调用期间,我的mAuth变量出现“无此实例字段”错误,该变量设置为FirebaseAuth的实例.我试过看了这个,但我发现并尝试的东西似乎与我的情况无关.

如果有人有任何理由为什么这种方法不会做任何事情,我会很感激帮助,因为我在过去的4个小时里一直在撞墙,试图解决这个问题.

提前致谢.

更新我通过将所有firebase服务(包括google play)的版本从11.6.0更改为10.2.6来实现此目的.

解决方法:

调用signInWithEmailAndPassword()时遇到类似的问题.不是真正的答案,但我认为自从升级到Firebase 11.6.0以来已经出现这种情况.降级到11.4.2似乎删除了警告消息.

今天的关于工厂:获取Firebase简单登录的当前user.id电子邮件/密码的分享已经结束,谢谢您的关注,如果想了解更多关于Android Studio 的 Firebase 工具中缺少电子邮件和密码身份验证、android – Firebase Auth – 带电子邮件和密码 – 检查用户是否已注册、Android – Firebase快速入门电子邮件/密码验证演示不起作用、Android – Firebase身份验证无法使用电子邮件/密码设置的相关知识,请在本站进行查询。

本文标签: