这是我的模块配置并运行:
angular.module('FXBApp', [
'ui.bootstrap'
,'ui.router'
,'oc.lazyLoad'
,'parse-angular'
]).
config(['$urlRouterProvider','$stateProvider', '$ocLazyLoadProvider',
function($urlRouterProvider,$stateProvider,$ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
debug:false,
events:true
});
$urlRouterProvider.otherwise('/login');
$stateProvider
.state('login', {
url: "/login",
templateUrl: "views/login/login.html",
controller:'LoginCtrl',
resolve:{
loadMyFiles:function($ocLazyLoad){
return $ocLazyLoad.load({
name: 'FXBApp',
files: ['scripts/controllers/login.js']
})
}
}
})
.state('dashboard', {
url: "/dashboard",
templateUrl: "views/dashboard/main.html",
controller:'DashboardMainCtrl',
resolve:{
loadMyFiles:function($ocLazyLoad){
return $ocLazyLoad.load({
name: 'FXBApp',
files: [
'views/dashboard/dashboard.main.js'
,'views/dashboard/ribbon.js'
,'scripts/directives/sidebar/sidebar.js'
]
})
}
}
})
.state('dashboard.home', {
url: "/dashboard/home",
templateUrl: "views/dashboard/home.html",
controller:'DashboardHomeCtrl',
resolve:{
loadMyFiles:function($ocLazyLoad){
return $ocLazyLoad.load({
name: 'FXBApp',
files: [
'scripts/controllers/dashboard.home.js'
,'scripts/services/authentication.js'
,'scripts/directives/stats/stats.js'
,'scripts/services/dbstats.js'
]
})
}
}
})
run(['$rootScope','$state', function ($rootScope,$state) {
Parse.initialize("$$$$$$$", "######");
$rootScope.sessionUser ={};
$rootScope.isLoggedIn = function () {
if (!Parse.User.current()) $state.go('login');
};
$rootScope.logOut = function () {
Parse.User.logOut()
};
$rootScope.authUser = function (usr, pwd) {
//TODO: assure user is active and e-mail is verified
return Parse.User.logIn(usr,pwd).then(function (user) {
var qr = new Parse.Query(Parse.Role);
qr.equalTo('users', Parse.User.current().id);
return qr.first().then(function (e) {
try {
if (e.getName() != 'admin' && e.getName() != 'team_leader') {
return {err: true, userId: user.id, msg: 'User does not have permission to access'}
} else {
$rootScope.sessionUser = user;
return {err: false, userId: user.id}
}
} catch (e) {
return {err: true, userId: user.id, msg: 'User not assigned to any role--'+e}
}
}, function (e){
return {err: true, userId: user.id, msg:e.msg}
});
}, function (error) {
return {err:true,msg:error.message}
});
};
$rootScope.isLoggedIn()
}]);
如您所见,我正在使用 ui 路由器以及延迟加载来正确管理应用程序性能。
一旦登录序列从登录控制器使用 启动$rootScope.authUser($scope.usr,$scope.pwd),如果我使用then()close 附加到此调用,我可以控制台Parse.User.current()显示当前用户。
登录后路由工作正常,棘手的部分是当我尝试从成功登录后允许用户使用的 parse.com 类中获取数据时;它总是给出 403 错误。调查原因,我发现Parse.User.current()返回 null 同时$rootScope.sessionUser持有对登录用户的正确引用。
我尝试使用“Parse.Session.getSessionToken”,错误是“没有当前用户”。我尝试使用Parse.User.become($rootScope.sessionUsr.getSessionToken()),但事情变得更糟,因为现在我收到了无效的会话令牌错误,必须去 chrome 控制台从存储中删除 parse sdk 对象。
知道我做错了什么吗?