每当我第一次加载我的 AngularJS 应用程序时,我都会看到一个空白屏幕,这会尝试检查用户是否在一个永无止境的循环中进行了身份验证。
对于这个项目,我使用sails 0.11.0 版,用于前端的AngularJS 的最新版本,以及Bower 和Yeoman(特别是generator-cg-angular yeoman 生成器)。我也在使用 satellizer,它应该对这个过程有很大帮助。
我只是设置我的第一个登录和注册功能,所以我可能犯了一些简单的错误。
更新:来自 Sails Gitter 聊天室的一些乐于助人的人,我认为“没有在某处发送响应”——我被告知可能是错误的原因,来自一位经验丰富的开发人员。所以这可能是我的问题....还不知道,只是保持这个问题的更新。
我的一些相关代码如下:
这是我的应用程序尝试将用户发送到的第一个状态(在 app.js 文件中):
$stateProvider.state('dashboard', {
url: '/',
templateUrl: 'partial/scaffold/scaffold.html',
controller: 'ScaffoldCtrl as scaffold',
data: { loginRequired: true } // will also apply to all children of 'dashboard'
});
这些是我的 dashboard.auth 服务的内容,我知道这对于获得授权至关重要。
var currentUser,
isAuthenticated = false,
baseAuth = Restangular.all('user');
return {
login: function (credentials) {
return baseAuth.customPOST(credentials, 'login').then(function (user) {
$log.debug('User logged in: ', user);
isAuthenticated = true;
return user;
});
},
logout: function () {
return baseAuth.customPOST({}, 'logout').then(function () {
currentUser = undefined;
isAuthenticated = false;
});
},
register: function (newUser) {
return baseAuth.customPOST(newUser, 'register').then(function (user) {
isAuthenticated = true;
return user;
});
},
currentUser: function () {
return currentUser;
},
isAuthenticated: function (force) {
var deferred = $q.defer();
if (isAuthenticated && currentUser) {
deferred.resolve(currentUser);
} else {
return baseAuth.customGET('authenticated').then(function (user) {
deferred.resolve(currentUser);
isAuthenticated = true;
currentUser = user;
return user;
});
}
return deferred.promise;
}
};
后端控制器:
登记
register: function (req, res) {
RegistrationService.registerUser(req.body, function (err, user) {
if (err) { res.json(400, err); }
return res.json(user);
});
},
登录
login: function (req, res) {
passport.authenticate('user', function (err, user, info) {
if (err) { return res.json(err); }
else if (user) {
req.logIn(user, function (err) {
if (err) { return res.json(err); }
return res.json(user);
});
} else { return res.json(400, info); }
})(req, res);
},
然后后端有很多不同的代码,它们都应该是非常标准的,我尝试遵循我从教程中找到的最佳实践。如果您知道我的应用程序发生了什么,请告诉我。
这是当我转到运行应用程序的http://localhost:9001/#/时记录到控制台的错误(它在一个循环中不断地运行......)。
[数组[0]、“get”、“authenticated”、“ http://localhost:1337/user/authenticated ”] angular.js:12314 [“”、“get”、“authenticated”、“ http:// localhost:1337/user/authenticated "] angular.js:12314 [Array[0], "get", "authenticated", " http://localhost:1337/user/authenticated "] angular.js:12314 ["" , "get", "authenticated", " http://localhost:1337/user/authenticated "] angular.js:12314 [Array[0], "get", "authenticated", " http://localhost:1337 /user/authenticated "] angular.js:12314 ["", "get", "authenticated", " http://localhost:1337/user/authenticated "] angular.js:12314 [Array[0], "get", "authenticated", " http://localhost:1337/user/authenticated "]