Javascript AngularJs用户身份验证仅在第二次交互时发生
我有一个AngularJs用户身份验证,由我自己构建,并正在运行。一切正常。我可以登录,可以检查用户是否有权访问特定页面等。。除了我只在与WebApp进行第二次交互时运行令牌身份验证这一事实 例如,如果我更改了localStorage(保存用户数据的地方)上的一些数据,并尝试转到管理员页面,例如,我将能够访问该页面,但在下一次交互中,我将从该页面踢出并返回登录过程 可能会发生什么? 这是我正在使用的代码: app.js 同样的事情也发生在单个Javascript AngularJs用户身份验证仅在第二次交互时发生,javascript,angularjs,Javascript,Angularjs,我有一个AngularJs用户身份验证,由我自己构建,并正在运行。一切正常。我可以登录,可以检查用户是否有权访问特定页面等。。除了我只在与WebApp进行第二次交互时运行令牌身份验证这一事实 例如,如果我更改了localStorage(保存用户数据的地方)上的一些数据,并尝试转到管理员页面,例如,我将能够访问该页面,但在下一次交互中,我将从该页面踢出并返回登录过程 可能会发生什么? 这是我正在使用的代码: app.js 同样的事情也发生在单个函数上。假设我在页面上有一个重要的函数,所以只有管理员
函数上。假设我在页面上有一个重要的函数,所以只有管理员才能执行该函数。我也在检查这个过程中的身份验证,但它发生了同样的事情,只是在第二次交互中
功能:
$scope.debugTk = function() {
checkToken();
if (!userToken) {
alert('Authentication failed');
} else {
$http.get('dist/php/db.php?action=debugTk')
.success(function(res){
$scope.resultDebug = res;
}).error(function(err){
alert(feedbackError);
});
}
}
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
return $http.post('dist/php/db.php?action=token', userCheckToken).then(
function (res) {
userToken = res;
},
function (err) {
alert(feedbackError);
});
};
正如charlieftl已经说过的,checkToken函数使用一个默认异步的XHR请求。您需要使用如下回调来包装依赖于执行checkToken
的所有内容:
function getUser() {
userinfo = JSON.parse(localStorageService.get("user")); //convert string to json
$scope.userData = userinfo; //Display purpouso only;
};
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
$http.post('dist/php/db.php?action=token', userCheckToken)
.success(function(res){
return res; // returns inside a promise make them chainable.
}).error(function(err){
return feedbackError;
});
};
$rootScope.$on('$stateChangeStart', function(e, to) {
if (to.data && to.data.requireLogin) {
getUser();
if (!userinfo) {
e.preventDefault();
$state.go('login');
alert("You need to be loggedin");
} else {
checkToken().then(function(userToken){ // this gets executed after checkToken() succeeds.
if (!userToken) {
// this won't work here: e.preventDefault();
userInfo = false;
$state.go('login');
localStorageService.clearAll();
alert('Authentication failed');
}
}, function(err){
// this gets called when your .error method returns an error
// eg. the HTTP request failed.
});
}
}
});
您的debugTk
函数将如下所示:
$scope.debugTk=函数(){
checkToken()。然后(函数(){
//成功
},函数(){
//错误
if (!userToken) {
alert('Authentication failed');
} else {
$http.get('dist/php/db.php?action=debugTk')
.success(function(res){
$scope.resultDebug = res;
}).error(function(err){
alert(feedbackError);
});
}
});
}
请在此处阅读有关承诺的更多信息:
编辑:e.preventDefault()
调用在承诺内不起作用,您需要更改代码以适应承诺。我不会将类似的内容包装到$stateChangeStart事件中,而是使用服务来处理所有身份验证内容。正如charlieftl已经说过的,您的checkToken函数使用默认异步的XHR请求。您需要包装它依赖于执行checkToken
的任何东西,回调如下:
function getUser() {
userinfo = JSON.parse(localStorageService.get("user")); //convert string to json
$scope.userData = userinfo; //Display purpouso only;
};
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
$http.post('dist/php/db.php?action=token', userCheckToken)
.success(function(res){
return res; // returns inside a promise make them chainable.
}).error(function(err){
return feedbackError;
});
};
$rootScope.$on('$stateChangeStart', function(e, to) {
if (to.data && to.data.requireLogin) {
getUser();
if (!userinfo) {
e.preventDefault();
$state.go('login');
alert("You need to be loggedin");
} else {
checkToken().then(function(userToken){ // this gets executed after checkToken() succeeds.
if (!userToken) {
// this won't work here: e.preventDefault();
userInfo = false;
$state.go('login');
localStorageService.clearAll();
alert('Authentication failed');
}
}, function(err){
// this gets called when your .error method returns an error
// eg. the HTTP request failed.
});
}
}
});
您的debugTk
函数将如下所示:
$scope.debugTk=函数(){
checkToken()。然后(函数(){
//成功
},函数(){
//错误
if (!userToken) {
alert('Authentication failed');
} else {
$http.get('dist/php/db.php?action=debugTk')
.success(function(res){
$scope.resultDebug = res;
}).error(function(err){
alert(feedbackError);
});
}
});
}
请在此处阅读有关承诺的更多信息:
编辑:e.preventDefault()
调用在承诺内不起作用,您需要更改代码以适应承诺。我不会将类似的内容包装到$stateChangeStart事件中,而是使用服务来处理所有身份验证内容。正如charlieftl已经说过的,您的checkToken函数使用默认异步的XHR请求。您需要包装它依赖于执行checkToken
的任何东西,回调如下:
function getUser() {
userinfo = JSON.parse(localStorageService.get("user")); //convert string to json
$scope.userData = userinfo; //Display purpouso only;
};
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
$http.post('dist/php/db.php?action=token', userCheckToken)
.success(function(res){
return res; // returns inside a promise make them chainable.
}).error(function(err){
return feedbackError;
});
};
$rootScope.$on('$stateChangeStart', function(e, to) {
if (to.data && to.data.requireLogin) {
getUser();
if (!userinfo) {
e.preventDefault();
$state.go('login');
alert("You need to be loggedin");
} else {
checkToken().then(function(userToken){ // this gets executed after checkToken() succeeds.
if (!userToken) {
// this won't work here: e.preventDefault();
userInfo = false;
$state.go('login');
localStorageService.clearAll();
alert('Authentication failed');
}
}, function(err){
// this gets called when your .error method returns an error
// eg. the HTTP request failed.
});
}
}
});
您的debugTk
函数将如下所示:
$scope.debugTk=函数(){
checkToken()。然后(函数(){
//成功
},函数(){
//错误
if (!userToken) {
alert('Authentication failed');
} else {
$http.get('dist/php/db.php?action=debugTk')
.success(function(res){
$scope.resultDebug = res;
}).error(function(err){
alert(feedbackError);
});
}
});
}
请在此处阅读有关承诺的更多信息:
编辑:e.preventDefault()
调用在承诺内不起作用,您需要更改代码以适应承诺。我不会将类似的内容包装到$stateChangeStart事件中,而是使用服务来处理所有身份验证内容。正如charlieftl已经说过的,您的checkToken函数使用默认异步的XHR请求。您需要包装它依赖于执行checkToken
的任何东西,回调如下:
function getUser() {
userinfo = JSON.parse(localStorageService.get("user")); //convert string to json
$scope.userData = userinfo; //Display purpouso only;
};
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
$http.post('dist/php/db.php?action=token', userCheckToken)
.success(function(res){
return res; // returns inside a promise make them chainable.
}).error(function(err){
return feedbackError;
});
};
$rootScope.$on('$stateChangeStart', function(e, to) {
if (to.data && to.data.requireLogin) {
getUser();
if (!userinfo) {
e.preventDefault();
$state.go('login');
alert("You need to be loggedin");
} else {
checkToken().then(function(userToken){ // this gets executed after checkToken() succeeds.
if (!userToken) {
// this won't work here: e.preventDefault();
userInfo = false;
$state.go('login');
localStorageService.clearAll();
alert('Authentication failed');
}
}, function(err){
// this gets called when your .error method returns an error
// eg. the HTTP request failed.
});
}
}
});
您的debugTk
函数将如下所示:
$scope.debugTk=函数(){
checkToken()。然后(函数(){
//成功
},函数(){
//错误
if (!userToken) {
alert('Authentication failed');
} else {
$http.get('dist/php/db.php?action=debugTk')
.success(function(res){
$scope.resultDebug = res;
}).error(function(err){
alert(feedbackError);
});
}
});
}
请在此处阅读有关承诺的更多信息:
编辑:调用e.preventDefault()
无法在承诺内工作,您需要更改代码以适应承诺。我不会将类似的内容包装到$stateChangeStart事件中,而是使用服务来处理所有身份验证内容。根据AngularJS文档:
$http legacy promise方法success和error已被弃用。请改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy error
关于您的问题,正如前面所说的checkToken
函数是异步的,因此每次调用此函数时都需要使用promises
。返回$http.post
来自checkToken
函数的结果:
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
return $http.post('dist/php/db.php?action=token', userCheckToken).then(
function (res) {
userToken = res;
},
function (err) {
alert(feedbackError);
});
};
然后将其作为常规承诺:
$scope.debugTk = function() {
checkToken().then(function(){
if (!userToken) {
alert('Authentication failed');
} else {
//.....
}
});
}
根据AngularJS文件:
$http legacy promise方法success和error已被弃用。请改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy error
关于您的问题,正如前面所说的checkToken
函数是异步的,因此每次调用此函数时都需要使用promises
。返回$http.post
来自checkToken
函数的结果:
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
return $http.post('dist/php/db.php?action=token', userCheckToken).then(
function (res) {
userToken = res;
},
function (err) {
alert(feedbackError);
});
};
然后将其作为常规承诺:
$scope.debugTk = function() {
checkToken().then(function(){
if (!userToken) {
alert('Authentication failed');
} else {
//.....
}
});
}
根据AngularJS文件:
$http legacy promise方法success和error已被弃用。请改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy error
关于您的问题,正如前面所说的checkToken
函数是异步的,因此每次调用此函数时都需要使用promises
。返回$http.post
来自checkToken
函数的结果:
function checkToken() {
var userCheckToken = JSON.parse(localStorageService.get("user"));
return $http.post('dist/php/db.php?action=token', userCheckToken).then(
function (res) {
userToken = res;
},
function (err) {
alert(feedbackError);
});
};
然后将其作为常规承诺:
$scope.debugTk = function() {
checkToken().then(function(){
if (!userToken) {
alert('Authentication failed');
} else {
//.....
}
});
}
根据AngularJS文件:
$http legacy promise方法success和error已被弃用。请改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy error
关于您的问题,正如前面所说,checkToken
函数是异步的,因此您需要使用promises