每當我第一次加載我的AngularJS應用程序,我得到一個空白屏幕,並嘗試檢查用戶是否在永無止境的循環中進行身份驗證。初始啓動時獲得Authenticated循環
對於這個項目,我使用sails版本0.11.0,最新版本的AngularJS用於前端,而Bower和Yeoman(特別是generator-cg-angular yeoman generator)。我也在使用衛星,這應該對這個過程有很大的幫助。
我剛剛設置了我的第一個登錄和註冊功能,所以我可能犯了一些簡單的錯誤。
UPDATE:從一些有用的人對帆的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);
},
再有就是在後端有很多不同的代碼,這都應該是p retty標準,我試圖遵循我在教程中找到的最佳實踐。如果您知道我的應用程序正在發生什麼,請告訴我。
這是當我去http://localhost:9001/#/應用程序正在運行的時候,記錄在控制檯上的錯誤(它在循環中繼續前進......)。
[陣列[0], 「獲得」, 「認證」, 「http://localhost:1337/user/authenticated 」] angular.js:12314 [「」, 「GET」, 「認證」, 「http://localhost:1337/user/authenticated」] 角。 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」 0],「獲得」,「認證」泰德 「 」 http://localhost:1337/user/authenticated「]