Guest User

Untitled

a guest
Jul 5th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. angular
  5. .module('app')
  6. .factory('AuthenticationService', AuthenticationService);
  7.  
  8. AuthenticationService.$inject = ['$http', '$cookies', '$rootScope', '$timeout', 'UserService'];
  9. function AuthenticationService($http, $cookies, $rootScope, $timeout, UserService) {
  10. var service = {};
  11.  
  12. service.Login = Login;
  13. service.SetCredentials = SetCredentials;
  14. service.ClearCredentials = ClearCredentials;
  15.  
  16. return service;
  17.  
  18. function Login(username, password, callback) {
  19.  
  20. /* Dummy authentication for testing, uses $timeout to simulate api call
  21. ----------------------------------------------*/
  22. $timeout(function () {
  23. var response;
  24. UserService.GetByUsername(username)
  25. .then(function (user) {
  26. if (user !== null && user.password === password) {
  27. response = { success: true };
  28. } else {
  29. response = { success: false, message: 'Username or password is incorrect' };
  30. }
  31. callback(response);
  32. });
  33. }, 1000);
  34.  
  35. /* Use this for real authentication
  36. ----------------------------------------------*/
  37. //$http.post('/api/authenticate', { username: username, password: password })
  38. // .success(function (response) {
  39. // callback(response);
  40. // });
  41.  
  42. }
  43.  
  44. function SetCredentials(username, password) {
  45. var authdata = Base64.encode(username + ':' + password);
  46.  
  47. $rootScope.globals = {
  48. currentUser: {
  49. username: username,
  50. authdata: authdata
  51. }
  52. };
  53.  
  54. // set default auth header for http requests
  55. $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
  56.  
  57. // store user details in globals cookie that keeps user logged in for 1 week (or until they logout)
  58. var cookieExp = new Date();
  59. cookieExp.setDate(cookieExp.getDate() + 7);
  60. $cookies.putObject('globals', $rootScope.globals, { expires: cookieExp });
  61. }
  62.  
  63. function ClearCredentials() {
  64. $rootScope.globals = {};
  65. $cookies.remove('globals');
  66. $http.defaults.headers.common.Authorization = 'Basic';
  67. }
  68. }
  69.  
  70. // Base64 encoding service used by AuthenticationService
  71. var Base64 = {
  72.  
  73. keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  74.  
  75. encode: function (input) {
  76. var output = "";
  77. var chr1, chr2, chr3 = "";
  78. var enc1, enc2, enc3, enc4 = "";
  79. var i = 0;
  80.  
  81. do {
  82. chr1 = input.charCodeAt(i++);
  83. chr2 = input.charCodeAt(i++);
  84. chr3 = input.charCodeAt(i++);
  85.  
  86. enc1 = chr1 >> 2;
  87. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  88. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  89. enc4 = chr3 & 63;
  90.  
  91. if (isNaN(chr2)) {
  92. enc3 = enc4 = 64;
  93. } else if (isNaN(chr3)) {
  94. enc4 = 64;
  95. }
  96.  
  97. output = output +
  98. this.keyStr.charAt(enc1) +
  99. this.keyStr.charAt(enc2) +
  100. this.keyStr.charAt(enc3) +
  101. this.keyStr.charAt(enc4);
  102. chr1 = chr2 = chr3 = "";
  103. enc1 = enc2 = enc3 = enc4 = "";
  104. } while (i < input.length);
  105.  
  106. return output;
  107. },
  108.  
  109. decode: function (input) {
  110. var output = "";
  111. var chr1, chr2, chr3 = "";
  112. var enc1, enc2, enc3, enc4 = "";
  113. var i = 0;
  114.  
  115. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  116. var base64test = /[^A-Za-z0-9+/=]/g;
  117. if (base64test.exec(input)) {
  118. window.alert("There were invalid base64 characters in the input text.n" +
  119. "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='n" +
  120. "Expect errors in decoding.");
  121. }
  122. input = input.replace(/[^A-Za-z0-9+/=]/g, "");
  123.  
  124. do {
  125. enc1 = this.keyStr.indexOf(input.charAt(i++));
  126. enc2 = this.keyStr.indexOf(input.charAt(i++));
  127. enc3 = this.keyStr.indexOf(input.charAt(i++));
  128. enc4 = this.keyStr.indexOf(input.charAt(i++));
  129.  
  130. chr1 = (enc1 << 2) | (enc2 >> 4);
  131. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  132. chr3 = ((enc3 & 3) << 6) | enc4;
  133.  
  134. output = output + String.fromCharCode(chr1);
  135.  
  136. if (enc3 != 64) {
  137. output = output + String.fromCharCode(chr2);
  138. }
  139. if (enc4 != 64) {
  140. output = output + String.fromCharCode(chr3);
  141. }
  142.  
  143. chr1 = chr2 = chr3 = "";
  144. enc1 = enc2 = enc3 = enc4 = "";
  145.  
  146. } while (i < input.length);
  147.  
  148. return output;
  149. }
  150. };
  151.  
  152. (function () {
  153. 'use strict';
  154.  
  155. angular
  156. .module('app')
  157. .controller('LoginController', LoginController);
  158.  
  159. LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
  160. function LoginController($location, AuthenticationService, FlashService) {
  161. var vm = this;
  162.  
  163. vm.login = login;
  164.  
  165. (function initController() {
  166. // reset login status
  167. AuthenticationService.ClearCredentials();
  168. })();
  169.  
  170. function login() {
  171. vm.dataLoading = true;
  172. AuthenticationService.Login(vm.username, vm.password, function (response) {
  173. if (response.success) {
  174. AuthenticationService.SetCredentials(vm.username, vm.password);
  175. $location.path('/');
  176. } else {
  177. FlashService.Error(response.message);
  178. vm.dataLoading = false;
  179. }
  180. });
  181. };
  182. }
  183.  
  184. })();
  185.  
  186. <div class="col-md-6 col-md-offset-3">
  187. <h2>Login</h2>
  188. <form name="form" ng-submit="vm.login()" role="form">
  189. <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
  190. <label for="username">Username</label>
  191. <input type="text" name="username" id="username" class="form-control" ng-model="vm.username" required />
  192. <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
  193. </div>
  194. <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
  195. <label for="password">Password</label>
  196. <input type="password" name="password" id="password" class="form-control" ng-model="vm.password" required />
  197. <span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
  198. </div>
  199. <div class="form-actions">
  200. <button type="submit" ng-disabled="form.$invalid || vm.dataLoading" class="btn btn-primary">Login</button>
  201. <img ng-if="vm.dataLoading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
  202. <a href="#!/register" class="btn btn-link">Register</a>
  203. </div>
  204. </form>
  205. </div>
Add Comment
Please, Sign In to add comment