我正在寻找一种可靠的方式来在所有可用控制器中使用“当前用户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提供任何有用的建议! 不知道其他人如何解决这个问题!
所以这是我解决这个确切问题的方法。我正在为我的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的此分支中找到它。
随意分叉,在本地安装和运行它,或者即使您愿意也可以对其做出贡献。我可以自己帮忙:)
希望这对您有帮助!!
Firebase 简单登录提供电子邮件/密码选项,我该如何使用它?从创建用户、为该用户存储数据开始,到登录和注销。
null 我需要重新验证用户,最好的方法是什么?在用户登录后,如何切换的状态? 谢谢
我正在使用Apple sign in将用户登录到我的应用程序,然后在Firebase上进行身份验证。现在,有时用户不分享他们的电子邮件,在这种情况下,苹果提供了一个虚构的电子邮件。登录后,am通过先用Firebase验证,将电子邮件存储在数据库中。我更新虚构的电子邮件与一个真实的首先在firebase,然后发送验证电子邮件。在firebase上更新主电子邮件后,用户是否可以再次登录苹果,因为苹果仍
我有一个flutter应用程序,我试图使用firebase电子邮件无密码登录。发送部分正在工作,但当我打开链接时,我得到一个错误: 但是,actioncode设置中的handleCodeInApp参数为true。 Uri在这里创建: FirebaseAuth中的isSignInWithEmailLink检查在我使用的^0.20.0+1版本中不能正常工作。有关如何修复此问题,请参考https://g
我正在使用Firebase以及作为Firebase动态链接的一部分生成的url。 > firebase动态url如下所示:例如:https://myapp.page.link/h1c4 用户收到电子邮件并点击验证链接。 在重定向结束时,我可以看到url包含如下所示的oobCode: https://myapp.page.link/h1c4?apikey=&oobcode=yg2n2elu4qgbs
问题内容: 我希望django通过电子邮件而非用户名对用户进行身份验证。一种方法是提供电子邮件值作为用户名值,但我不希望这样。原因是,我有一个,所以我不能有一个。 另一个原因是所有电子邮件都是唯一的,但有时用户名已被使用。因此,我将自动创建用户名为。 我该如何更改才能让Django通过电子邮件进行身份验证? 这就是我创建用户的方式。 这就是我的登录方式。 除了先获取用户名外,登录还有其他方法吗?