Home > Article > Web Front-end > Detailed explanation of interaction and verification steps when AngularJs users log in
This time I will bring you AngularJsDetailed explanation of the interaction and verification steps when the user logs in, what are the precautions for the interaction and verification when the AngularJs user logs in , the following is a practical case, let’s take a look.
1. Static page construction and ng’s formForm verificationimplementation:
<p class="register-frame-all"> <p class="register-frame"> <p class="register-msg"> <i></i> <form name="loginForm" ng-submit="loginAction()"> <p class="form-group"> <p class="input-group"> <span class="input-group-addon register-user"></span> <input autocomplete="off" type="number" class="form-control" placeholder="请输入手机号" required ng-model="loginData.loginName" name="loginName"> </p> <p class="input-group"> <span class="input-group-addon register-pwd"></span> <input type="password" class="form-control" placeholder="请输入密码" required ng-model="loginData.pwd" name="pwd"> </p> <button type="submit" class="btn btn-block btn-danger" ng-disabled="!( (loginForm.loginName.$valid) && (loginForm.pwd.$valid) )">登录</button> <em></em> </p> </form> </p> <p class="register-pic" ng-style="registerRnum"></p> </p> </p>
2. Define the controller for user login, and use the http service to process the login interface:
$http({ url:G.apiUrl_dl+'loginByPhone', method:'post', data:{ 'phone':loginName, 'pwd':pwd }, headers:{'Content-Type':'application/x-www-form-urlencoded'}, transformRequest: function(obj) { var str = []; for(var p in obj){ str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); } return str.join("&"); } }).success(function(data){ // 登录成功后的操作...18 19 });
in the controller. 3. If the login is successful, save the user's data to cookie or session and use $state service to jump to the specified page:
// 登录成功 if($scope.loginActionData.token){ sessionStorage.setItem("token", $scope.loginActionData.token); sessionStorage.setItem("tsname", $scope.loginActionData.name); sessionStorage.setItem("rights", $scope.loginActionData.rights); sessionStorage.setItem("userId", $scope.loginActionData.userId); sessionStorage.setItem("departmentsId", $scope.loginActionData.departmentsId); sessionStorage.setItem("departmentsName", $scope.loginActionData.departmentsName); $state.go('index'); }else{ // 登录失败的弹框提示 $('#loginAction').modal('show'); }
4. The next step is to prevent users from skipping the login page through other methods (such as directly entering the address in the address bar to enter the page):
I put the operation of this method in the run method that is executed first by the controller as mentioned before. Every time before entering a page, it will check whether the user is logged in legally. If it is not logged in legally, we will let him jump. Go to the login page
angular.module.run(['$rootScope','$state',function($rootScope,$state){ $rootScope.$on('$stateChangeStart',function(event,toState){ // 防止FQ if(!(sessionStorage.getItem("token")))$state.go('register'); }); }]);
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of Detailed explanation of interaction and verification steps when AngularJs users log in. For more information, please follow other related articles on the PHP Chinese website!