|
16 | 16 | var authenticated = false; |
17 | 17 |
|
18 | 18 | return { |
19 | | - login: function (user, password) { |
20 | | - return $http.post('/api/login', { |
| 19 | + login: function (user, password, otp) { |
| 20 | + var data = { |
21 | 21 | password: password, |
22 | 22 | user: user |
23 | | - }).then(function (result) { |
| 23 | + }; |
| 24 | + |
| 25 | + if (otp !== null) { |
| 26 | + data.otp = otp; |
| 27 | + } |
| 28 | + |
| 29 | + return $http.post('/api/login', data).then(function (result) { |
24 | 30 | $rootScope.session = result; |
25 | 31 | $rootScope.$broadcast('loginSucceeded', result); |
26 | 32 | if (!authenticated) { |
|
29 | 35 | } |
30 | 36 | return result; |
31 | 37 | }, function (error) { |
32 | | - $rootScope.$broadcast('loginFailed', error); |
| 38 | + if (error.status === 499) { |
| 39 | + $rootScope.$broadcast('otpRequired'); |
| 40 | + } else { |
| 41 | + $rootScope.$broadcast('loginFailed', error); |
| 42 | + } |
33 | 43 | return error; |
34 | 44 | }); |
35 | 45 | }, |
|
50 | 60 | }; |
51 | 61 | }]); |
52 | 62 |
|
53 | | - app.controller('AuthenticateController', ['$http', '$scope', '$window', 'AuthenticationService', function ($http, $scope, $window, AuthenticationService) { |
| 63 | + app.controller('AuthenticateController', ['$http', '$scope', '$timeout', '$window', 'AuthenticationService', function ($http, $scope, $timeout, $window, AuthenticationService) { |
54 | 64 | $scope.authenticate = { |
| 65 | + cancel: function () { |
| 66 | + $scope.authenticate.otp = null; |
| 67 | + $timeout(function () { document.getElementById("authenticate-user").focus(); }); |
| 68 | + }, |
| 69 | + otp: null, |
55 | 70 | submit: function () { |
56 | | - AuthenticationService.login($scope.authenticate.user, $scope.authenticate.password); |
| 71 | + AuthenticationService.login($scope.authenticate.user, $scope.authenticate.password, $scope.authenticate.otp); |
57 | 72 | } |
58 | 73 | }; |
59 | 74 |
|
60 | 75 | $scope.$on('loginSucceeded', function () { |
61 | 76 | $window.location.reload(true); |
62 | 77 | }); |
63 | 78 |
|
| 79 | + $scope.$on('otpRequired', function () { |
| 80 | + $scope.authenticate.otp = ''; |
| 81 | + $timeout(function () { document.getElementById("authenticate-otp").focus(); }); |
| 82 | + }); |
| 83 | + |
64 | 84 | $scope.$on('loginFailed', function () { |
65 | 85 | $scope.authenticate.error = "Felaktigt användarnamn och/eller lösenord."; |
66 | 86 | }); |
|
75 | 95 | <div class="panel-body"> |
76 | 96 | <div class="form-group"> |
77 | 97 | <label for="authenticate-user">E-postadress</label> |
78 | | - <input autofocus class="form-control" id="authenticate-user" ng-model="authenticate.user" placeholder="E-postadress" required type="text"> |
| 98 | + <input autofocus class="form-control" id="authenticate-user" ng-disabled="authenticate.otp !== null" ng-model="authenticate.user" placeholder="E-postadress" required type="text"> |
79 | 99 | </div> |
80 | 100 | <div class="form-group"> |
81 | 101 | <label for="authenticate-password">Lösenord</label> |
82 | | - <input class="form-control" id="authenticate-password" ng-model="authenticate.password" placeholder="Lösenord" required type="password"> |
| 102 | + <input class="form-control" id="authenticate-password" ng-disabled="authenticate.otp !== null" ng-model="authenticate.password" placeholder="Lösenord" required type="password"> |
| 103 | + </div> |
| 104 | + <div class="form-group" ng-if="authenticate.otp !== null"> |
| 105 | + <label for="authenticate-otp">SMS-kod</label> |
| 106 | + <input autocomplete="off" class="form-control" id="authenticate-otp" ng-model="authenticate.otp" placeholder="SMS-kod" required type="password"> |
83 | 107 | </div> |
84 | 108 | <uib-alert ng-if="authenticate.error" style="margin-bottom: 15px;" type="danger">{{authenticate.error}}</uib-alert> |
85 | 109 | <button class="btn btn-primary" ng-disabled="!authenticate.form.$valid" type="submit">Logga in</button> |
| 110 | + <button class="btn btn-warning" ng-click="authenticate.cancel()" ng-if="authenticate.otp !== null" style="float: right;" type="button">Avbryt</button> |
86 | 111 | </div> |
87 | 112 | </form> |
88 | 113 | </div> |
|
0 commit comments