AngularJS和函数顺序检查认证用户(Facebook API)
我的代码大部分来自web,但我不想在登录用户登录后重定向他们,也不想将未登录用户重定向到登录页面。我一直在尝试AngularJS和函数顺序检查认证用户(Facebook API),facebook,angularjs,facebook-javascript-sdk,Facebook,Angularjs,Facebook Javascript Sdk,我的代码大部分来自web,但我不想在登录用户登录后重定向他们,也不想将未登录用户重定向到登录页面。我一直在尝试resolve,但检查用户是否被授权的服务方法总是显示他没有被授权(在解析中),然后当页面被加载时,用户正在被验证。我正在引导我的应用程序,所以我想我会先从Facebook获取信息,然后再加载应用程序 index.php(脚本部分) <script> window.fbAsyncInit = function() { FB.init({
resolve
,但检查用户是否被授权的服务方法总是显示他没有被授权(在解析中),然后当页面被加载时,用户正在被验证。我正在引导我的应用程序,所以我想我会先从Facebook获取信息,然后再加载应用程序
index.php(脚本部分)
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '191392907718296',
channelUrl : '//'+window.location.hostname+'/channel.html',
status : true,
cookie : true,
xfbml : false
});
angular.bootstrap(document, ['myApp','ngRoute']);
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
var app = angular.module('myApp',['ngRoute']);
app.config( function($routeProvider) {
$routeProvider.
when('/login', {
controller : 'MainCtrl',
templateUrl : 'views/first.html',
}).
when('/', {
controller : 'SecondCtrl',
templateUrl : 'views/second.html',
resolve : {
checkUser : function($location,FBUser)
{
console.log(FBUser.authorized);
$location.path('/login');
}
}
});
});
app.controller('MainCtrl', ['$scope', 'FBUser',function($scope, FBUser) {
$scope.user = FBUser;
}]);
app.controller('SecondCtrl', ['$scope',function($scope) {
}]);
app.service('facebook', ['$rootScope', '$window', function ($rootScope, $window) {
this.askFacebookForAuthentication = function (fail, success) {
FB.login(function (response) {
$rootScope.$apply(function () {
if (response.authResponse) {
FB.api('/me', success);
} else {
fail('User cancelled login or did not fully authorize.');
}
});
});
};
this.getLoginStatus = function () {
FB.getLoginStatus(function (response) {
return response;
});
};
this.FB = $window.FB;
}]);
/* --------------- FB API MODELS ------------------ */
app.service('FBUser', ['$log', '$rootScope', 'facebook', function ($log, $rootScope, facebook,$location) {
var that = this;
this.authorized = false;
facebook.FB.Event.subscribe('auth.authResponseChange', function (response) {
$log.info("Event: auth.authResponseChange");
if (response.authResponse) {
if (response.status === 'connected') {
// User logged in and authorized
$log.info('User logged in and authorized');
$rootScope.$apply(function () {
that.authorized = true;
});
$rootScope.$apply( function() {
redirectTo('/');
});
} else if (response.status === 'not_authorized') {
// User logged in but has not authorized app
$log.info('User logged in');
$rootScope.$apply(function () {
that.authorized = false;
});
} else {
// User logged out
$log.info('User logged out');
$rootScope.$apply(function () {
that.authorized = false;
});
}
} else {
$log.info('No valid authResponse found, user logged out');
$rootScope.$apply(function () {
that.authorized = false;
});
}
});
this.login = function (success, fail) {
facebook.FB.login(function (response) {
$rootScope.$apply(function () {
if (response.authResponse) {
success(response);
} else {
fail('Login unsuccessful');
}
});
});
};
this.logout = function () {
facebook.FB.logout(function () {
$rootScope.$apply(function () {
that.authorized = false;
});
});
};
}]);