vzh*_*hen 2 angularjs firebase angularjs-service firebase-security
晕,所有,
我想使用angularjs与firebase简单登录(facebook).但我不知道如何创建auth共享服务.
我想做的是
$locationif user loggedin/not-login我也是angularjs的新手,但我不知道在这种情况下我应该使用哪些服务.
service还是factory?
如何将下面的代码放在角度服务中,然后告诉每个控制器用户是否登录?
var firebaseRef = new Firebase("https://test.firebaseio.com");
var auth = new FirebaseAuthClient(firebaseRef, function(error, user) {
if (user) {
console.log(user);
} else if (error) {
console.log(error);
} else {
console.log('user not login');
}
});
Run Code Online (Sandbox Code Playgroud)
这是我猜的,如果存在user则从authService控制器中返回值authService.user然后重定向到登录页面,否则显示登录对话框,使用登录按钮调用以下代码
authService.login('facebook');
Run Code Online (Sandbox Code Playgroud)
如果我可以这样做,或者有更好的方法,请告诉我?
这是我到目前为止使用的...
我还没有实现重定向,但其余的工作.
p4pApp.factory('firebaseAuth', function($rootScope) {
var auth = {},
FBref = new Firebase(p4pApp.FIREBASEPATH);
auth.broadcastAuthEvent = function() {
$rootScope.$broadcast('authEvent');
};
auth.client = new FirebaseAuthClient(FBref, function(error, user) {
if (error) {
} else if (user) {
auth.user = user;
auth.broadcastAuthEvent();
} else {
auth.user = null;
auth.broadcastAuthEvent();
}
});
auth.login = function() {
this.client.login('facebook');
};
auth.logout = function() {
this.client.logout();
};
return auth;
});
Run Code Online (Sandbox Code Playgroud)
AuthCtrl对我的所有/大部分页面都是通用的.
var AuthCtrl = function($scope, firebaseAuth) {
$scope.login = function() {
firebaseAuth.login();
};
$scope.logout = function() {
firebaseAuth.logout();
};
$scope.isLoggedIn = function() {
return !!$scope.user;
};
// src: Alex Vanston (https://coderwall.com/p/ngisma)
$scope.safeApply = function(fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};
$scope.$on('authEvent', function() {
$scope.safeApply(function() {
$scope.user = firebaseAuth.user;
});
});
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1597 次 |
| 最近记录: |