我正在尝试通过向服务器发出http请求来检查用户是否已登录(本地保存了一些令牌)。
我有另一个安全服务,用于执行有关身份验证的所有内容。
Private Sub Form_Load()
Dim salary As Double
salary = CurrentDb.OpenRecordset("SELECT MAX(Salary) FROM Employee")(0)
salary = salary + (1000 / 23)
total_salary.Value = salary
End Sub
,安全服务如下所示:
(function() {
var myApp = angular.module("app");
fetchData().then(bootstrapApplication);
function fetchData() {
var initInjector = angular.injector(["ng"]);
var $http = initInjector.get("$http");
//makes a request to currentUser using token from $localStorage
var localData = JSON.parse(window.localStorage.getItem('authorizationData'));
//if token exists in localStorage it adds a provider
if (localData && localData.token) {
//adds the header
$http.defaults.headers.common.Authorization = 'Bearer ' + localData.token;
//makes call
}
return $http.get("http://demo-site/current-user")
.then(function(response) {
// successfull, adds currentUser as a constant
angular.module('security').config(['authServiceProvider', function (authServiceProvider) {
authService().authenticate(response.data)
}]);
}, function(errorResponse) {
// Handle error case
});
}
function bootstrapApplication() {
angular.element(document).ready(function() {
angular.bootstrap(document, ["app"]);
});
}
}());
当用户成功登录时,将调用authenticate方法。 (第一次登录也会返回要在本地保存的令牌,之后只在authService中设置currentUser。
如何在bootstrap之前收到http响应后设置currentUser并将其保留在应用程序中?
这是我写的部分,请求成功:
angular.module('security', ['ngNewRouter'])
// .factory('authService', authService);
.service('authService', ['$http', '$q', '$window', 'CONSTANTS', '$location', '$router', authService ]);
function authService($http, $q, $window, CONSTANTS, $location, $router) {
var service = {};
var r = $router;
//successfull data either from login or currentUser
var _authenticate = function(data) {
if (is('String', data.token)) {
var lsData = {
shopId: data.response.id,
token: data.token
}
$window.localStorage.setItem('authorizationData', JSON.stringify(lsData));
}
if (is('Object', data.response) && data.response.id)
service.currentUser = data.response;
}
...
service.authenticate = _authenticate;
return service;
}