Angularjs 如何在angular js中自动登录?
我有一个登录屏幕,我正在使用一些硬线数据进行登录Angularjs 如何在angular js中自动登录?,angularjs,ionic-framework,Angularjs,Ionic Framework,我有一个登录屏幕,我正在使用一些硬线数据进行登录 angular.module('starter.controllers', []) .controller('AppCtrl', function($scope, $rootScope, $window, $ionicModal, $timeout, authService, $state, $http, $ionicLoading, $location) { //$window.loc
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $rootScope, $window, $ionicModal, $timeout, authService, $state, $http, $ionicLoading, $location) {
//$window.location.reload();
$scope.loginSubmitted = false;
$scope.myflag = false;
$scope.User = {};
$scope.toast = function() {
$ionicLoading.show({
template: 'wrong credentials'
});
$timeout(function() {
$ionicLoading.hide();
}, 1000);
}
$scope.footerflag = true;
$scope.hidefooter = function() {
$timeout(function() {
$scope.footerflag = false;
},1)
}
$scope.showfooter = function() {
$timeout(function() {
$scope.footerflag = true;
},1)
}
$scope.doLogin = function() {
console.log("trying login");
// // var res = $http.post('http://login.com/postLogin', $scope.user);
// authService.postdetails($scope.User).success(function(data, status, headers, config) {
// $scope.message = data;
// console.log("succesfn");
// console.log(status);
//
// })
// .error(function(data, status, headers, config) {
// alert("failure message: " + JSON.stringify({
// data: data
// }));
// console.log(fail);
// });
$scope.loginSubmitted = true;
$scope.loginstatus = 0;
authService.GetByUsername().success(function(data) {
$scope.UserData = data;
// console.log($scope.UserData);
for (var i = 0; i < $scope.UserData.length; i++) {
if ($scope.UserData[i].UserName == $scope.User.UserName && $scope.UserData[i].Password == $scope.User.Password) {
authService.currentuser = $scope.User.UserName;
//console.log(authService.currentuser);
$scope.loginstatus = 1;
break;
}
}
if ($scope.loginstatus == 1) {
// var lastVal = $cookieStore.get($scope.User.UserName);
// console.log(lastVal);
//$location.path('/app/playlists');
$scope.loginstatus = 0;
$state.go('app.playlists', null, {
reload: true
});
} else {
console.log('wrong credentials');
$scope.toast();
}
}).error(function(err) {
console.log(err);
});
}
});
角度模块('starter.controllers',[])
.controller('AppCtrl',函数($scope、$rootScope、$window、$IonicModel、$timeout、authService、$state、$http、$ionicLoading、$location){
//$window.location.reload();
$scope.loginSubmitted=false;
$scope.myflag=false;
$scope.User={};
$scope.toast=函数(){
$ionicLoading.show({
模板:“错误的凭据”
});
$timeout(函数(){
$ionicLoading.hide();
}, 1000);
}
$scope.footerflag=true;
$scope.hidefooter=函数(){
$timeout(函数(){
$scope.footerflag=false;
},1)
}
$scope.showfooter=函数(){
$timeout(函数(){
$scope.footerflag=true;
},1)
}
$scope.doLogin=函数(){
log(“尝试登录”);
////var res=$http.post('http://login.com/postLogin“,$scope.user);
//authService.postdetails($scope.User).success(函数(数据、状态、标题、配置){
//$scope.message=数据;
//console.log(“succesfn”);
//控制台日志(状态);
//
// })
//.error(函数(数据、状态、标题、配置){
//警报(“失败消息:+JSON.stringify({
//数据:数据
// }));
//控制台日志(失败);
// });
$scope.loginSubmitted=true;
$scope.loginstatus=0;
authService.GetByUsername()成功(函数(数据){
$scope.UserData=数据;
//log($scope.UserData);
对于(变量i=0;i<$scope.UserData.length;i++){
如果($scope.UserData[i]。用户名==$scope.User.UserName&&$scope.UserData[i]。密码==$scope.User.Password){
authService.currentuser=$scope.User.UserName;
//console.log(authService.currentuser);
$scope.loginstatus=1;
打破
}
}
如果($scope.loginstatus==1){
//var lastVal=$cookieStore.get($scope.User.UserName);
//console.log(lastVal);
//$location.path('/app/playlists');
$scope.loginstatus=0;
$state.go('app.playlists',空{
重新加载:正确
});
}否则{
console.log(“错误的凭证”);
$scope.toast();
}
}).error(函数(err){
控制台日志(err);
});
}
});
所以我想启用自动登录,直到用户单击注销按钮。如何做到这一点?
我只是在用户名和密码匹配时重定向到另一个页面。您可以检查用户是否登录到
模块.run()
方法。以下是一个例子:
这可能会对你有所帮助
请点击以下链接