Commit cf2b3498372e8b4b739d8723aa084b756fea661b

Authored by Rishav
1 parent 9835dbe3ed
Exists in master

login authenticate flow

app/views/login/login.controller.js
1 'use strict'; 1 'use strict';
2 2
3 //Load controller 3 //Load controller
4 angular.module('acuefuel') 4 angular.module('acuefuel')
5 5
6 .controller('LoginController', function ($scope, $state, LoginService) { 6 .controller('LoginController', function ($scope, $state, LoginService) {
7 7
8 $scope.user = {}; 8 $scope.user = {};
9 $scope.userData = function(){ 9 $scope.userData = function(){
10 10
11 var loginData = "username=" + $scope.user.username + "&password=" + $scope.user.password; 11 var loginData = "username=" + $scope.user.username + "&password=" + $scope.user.password;
12 LoginService.loginUser(loginData).then(function(result) { 12 LoginService.loginUser(loginData).then(function(result) {
13 console.log(result) 13 LoginService.authenticate();
14 $state.go('index.dashboard'); 14 LoginService.setAuth(true);
15 //$state.go('index.dashboard'); 15
16 // if(typeof result == 'object') {
17 // // LoginService.authenticate();
18 // // LoginService.setAuth(true);
19 // // $rootScope.path = true;
20 // $state.go('index.dashboard');
21 // // var reqPwdChng = localStorage.getItem("requiredChangePwd");
22 // // if (reqPwdChng && reqPwdChng == "Y") {
23 // // $state.go('resetPassword');
24 // // } else {
25 // // $state.go('app.upload');
26 // // }
27 // } else {
28 // //toastr.info("Error in login. Please check login name and password");
29 // }
30 }) 16 })
31 17
32 } 18 }
33 19
34 }); 20 });
app/views/login/login.html
1 <div class="middle-box text-center loginscreen animated fadeInDown"> 1 <div class="middle-box text-center loginscreen animated fadeInDown">
2 <div> 2 <div>
3 <div> 3 <div>
4 <h1 class="logo-name">AF+</h1> 4 <h1 class="logo-name">AF+</h1>
5 </div> 5 </div>
6 <h3>Welcome to ACUFUEL+</h3> 6 <h3>Welcome to ACUFUEL+</h3>
7 <!-- <p>Perfectly designed and precisely prepared admin theme with over 50 pages with extra new web app views. 7 <!-- <p>Perfectly designed and precisely prepared admin theme with over 50 pages with extra new web app views.
8 </p> --> 8 </p> -->
9 <p>Login in. To see it in action.</p> 9 <p>Login in. To see it in action.</p>
10 <form class="m-t" role="form" action="#"> 10 <form class="m-t" role="form">
11 <div class="form-group"> 11 <div class="form-group">
12 <input type="text" class="form-control" placeholder="Username" required="" ng-model="user.username"> 12 <input type="text" class="form-control" placeholder="Username" required="" ng-model="user.username" required>
13 </div> 13 </div>
14 <div class="form-group"> 14 <div class="form-group">
15 <input type="password" class="form-control" placeholder="Password" required="" ng-model="user.password"> 15 <input type="password" class="form-control" placeholder="Password" required="" ng-model="user.password" required>
16 </div> 16 </div>
17 <button type="submit" class="btn btn-primary block full-width m-b" ng-click="userData()">Login</button> 17 <button class="btn btn-primary block full-width m-b" ng-click="userData()">Login</button>
18 18
19 <!-- <a ui-sref="forgot_password"><small>Forgot password?</small></a> 19 <!-- <a ui-sref="forgot_password"><small>Forgot password?</small></a>
20 <p class="text-muted text-center"><small>Do not have an account?</small></p> 20 <p class="text-muted text-center"><small>Do not have an account?</small></p>
21 <a class="btn btn-sm btn-white btn-block" ui-sref="register">Create an account</a> --> 21 <a class="btn btn-sm btn-white btn-block" ui-sref="register">Create an account</a> -->
22 </form> 22 </form>
23 <!-- <p class="m-t"> <small>Inspinia we app framework base on Bootstrap 3 &copy; 2014</small> </p> --> 23 <!-- <p class="m-t"> <small>Inspinia we app framework base on Bootstrap 3 &copy; 2014</small> </p> -->
24 </div> 24 </div>
25 </div> 25 </div>
app/views/login/login.service.js
1 (function() { 1 (function() {
2 'use strict'; 2 'use strict';
3 3
4 angular.module('acuefuel') 4 angular.module('acuefuel')
5 .service('LoginService', ['$rootScope', '$q', '$http', 'BE', LoginService]) 5 .service('LoginService', ['$rootScope', '$q', '$http', 'BE', '$state', LoginService])
6 6
7 function LoginService($rootScope, $q, $http, BE) { 7 function LoginService($rootScope, $q, $http, BE, $state) {
8 8
9 this.loginUser = function(data) { 9 this.loginUser = function(data) {
10 10
11 var deferred = $q.defer(); 11 var deferred = $q.defer();
12 $http({ 12 $http({
13 method : 'POST', 13 method : 'POST',
14 url : BE.url +'/login', 14 url : BE.url +'/login',
15 headers : {'Content-Type': 'application/x-www-form-urlencoded'}, 15 headers : {'Content-Type': 'application/x-www-form-urlencoded'},
16 data : data 16 data : data
17 }) 17 })
18 .success(function(result) { 18 .success(function(result) {
19 deferred.resolve(result.data); 19 deferred.resolve(result.data);
20 }) 20 })
21 return deferred.promise; 21 return deferred.promise;
22 } 22 }
23 23
24 this.authenticate = function() { 24 this.authenticate = function() {
25 var deferred = $q.defer(); 25 var deferred = $q.defer();
26 $http({ 26 $http({
27 method : 'GET', 27 method : 'GET',
28 url : BE.url+'user/authenticate', 28 url : BE.url+'/user/authenticate',
29 headers : {'Content-Type': 'application/x-www-form-urlencoded'} 29 headers : {'Content-Type': 'application/x-www-form-urlencoded'}
30 }).then(function (result){ 30 }).then(function (result){
31 console.log(result) 31 console.log(result)
32 localStorage.setItem('userProfileId', result.data.userProfile.id); 32 if(result.data.user.admin == true){
33 localStorage.setItem('email', result.data.userProfile.email); 33 window.localStorage.setItem('loginId', result.data.id);
34 window.localStorage.setItem('loginData', JSON.stringify(result.data.userProfile));
35 $state.go('index.dashboard');
36 }else{
37 localStorage.clear();
38 toastr.info("Unauthorized");
39 }
34 deferred.resolve(result.data); 40 deferred.resolve(result.data);
35 },function (result){ 41 },function (result){
36 deferred.resolve(result.data); 42 deferred.resolve(result.data);
37 }); 43 });
38 return deferred.promise; 44 return deferred.promise;
39 } 45 }
40 46
41 this.setAuth = function(data) { 47 this.setAuth = function(data) {
42 localStorage.setItem('loginStatus', data); 48 localStorage.setItem('loginStatus', data);
43 } 49 }
50
51 this.logout = function(data) {
52
53 var deferred = $q.defer();
54 $http({
55 method : 'POST',
56 url : BE.url +'/user/logout',
57 headers : {'Content-Type': 'application/x-www-form-urlencoded'},
58 data : data
59 })
60 .success(function(result) {
61 deferred.resolve(result.data);
62 })
63 return deferred.promise;
64 }
44 } 65 }
45 66
46 })(); 67 })();