Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.module('Authentication')
  4.  
  5.  
  6. .controller('LoginController',
  7. ['$scope', '$rootScope', '$location', 'AuthenticationService',
  8. function ($scope, $rootScope, $location, AuthenticationService) {
  9. // reset login status
  10. AuthenticationService.ClearCredentials();
  11.  
  12. $scope.login = function () {
  13. $scope.dataLoading = true;
  14. AuthenticationService.Login($scope.username, $scope.password, function(response) {
  15. if(response.success) {
  16. AuthenticationService.SetCredentials($scope.username, $scope.password);
  17. $location.path('/');
  18. } else {
  19. $scope.error = response.message;
  20. $scope.dataLoading = false;
  21. }
  22. });
  23. };
  24. }]);
  25.  
  26. 'use strict';
  27.  
  28. angular.module('Authentication')
  29.  
  30. .factory('AuthenticationService',
  31. ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
  32. function (Base64, $http, $cookieStore, $rootScope, $timeout) {
  33. var service = {};
  34.  
  35. service.Login = function (username, password, callback) {
  36.  
  37. /* Dummy authentication for testing, uses $timeout to simulate api call
  38. ----------------------------------------------*/
  39. $timeout(function(){
  40. var response = { success: username === 'test' && password === 'test' };
  41. if(!response.success) {
  42. response.message = 'Username or password is incorrect';
  43. }
  44. callback(response);
  45. }, 1000);
  46.  
  47.  
  48. /* Use this for real authentication
  49. ----------------------------------------------*/
  50. //$http.post('/api/authenticate', { username: username, password: password })
  51. // .success(function (response) {
  52. // callback(response);
  53. // });
  54.  
  55. };
  56.  
  57. service.SetCredentials = function (username, password) {
  58. var authdata = Base64.encode(username + ':' + password);
  59.  
  60. $rootScope.globals = {
  61. currentUser: {
  62. username: username,
  63. authdata: authdata
  64. }
  65. };
  66.  
  67. $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line
  68. $cookieStore.put('globals', $rootScope.globals);
  69. };
  70.  
  71. service.ClearCredentials = function () {
  72. $rootScope.globals = {};
  73. $cookieStore.remove('globals');
  74. $http.defaults.headers.common.Authorization = 'Basic ';
  75. };
  76.  
  77. return service;
  78. }])
  79.  
  80. .factory('Base64', function () {
  81. /* jshint ignore:start */
  82.  
  83. var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  84.  
  85. return {
  86. encode: function (input) {
  87. var output = "";
  88. var chr1, chr2, chr3 = "";
  89. var enc1, enc2, enc3, enc4 = "";
  90. var i = 0;
  91.  
  92. do {
  93. chr1 = input.charCodeAt(i++);
  94. chr2 = input.charCodeAt(i++);
  95. chr3 = input.charCodeAt(i++);
  96.  
  97. enc1 = chr1 >> 2;
  98. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  99. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  100. enc4 = chr3 & 63;
  101.  
  102. if (isNaN(chr2)) {
  103. enc3 = enc4 = 64;
  104. } else if (isNaN(chr3)) {
  105. enc4 = 64;
  106. }
  107.  
  108. output = output +
  109. keyStr.charAt(enc1) +
  110. keyStr.charAt(enc2) +
  111. keyStr.charAt(enc3) +
  112. keyStr.charAt(enc4);
  113. chr1 = chr2 = chr3 = "";
  114. enc1 = enc2 = enc3 = enc4 = "";
  115. } while (i < input.length);
  116.  
  117. return output;
  118. },
  119.  
  120. decode: function (input) {
  121. var output = "";
  122. var chr1, chr2, chr3 = "";
  123. var enc1, enc2, enc3, enc4 = "";
  124. var i = 0;
  125.  
  126. // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  127. var base64test = /[^A-Za-z0-9+/=]/g;
  128. if (base64test.exec(input)) {
  129. window.alert("There were invalid base64 characters in the input text.n" +
  130. "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='n" +
  131. "Expect errors in decoding.");
  132. }
  133. input = input.replace(/[^A-Za-z0-9+/=]/g, "");
  134.  
  135. do {
  136. enc1 = keyStr.indexOf(input.charAt(i++));
  137. enc2 = keyStr.indexOf(input.charAt(i++));
  138. enc3 = keyStr.indexOf(input.charAt(i++));
  139. enc4 = keyStr.indexOf(input.charAt(i++));
  140.  
  141. chr1 = (enc1 << 2) | (enc2 >> 4);
  142. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  143. chr3 = ((enc3 & 3) << 6) | enc4;
  144.  
  145. output = output + String.fromCharCode(chr1);
  146.  
  147. if (enc3 != 64) {
  148. output = output + String.fromCharCode(chr2);
  149. }
  150. if (enc4 != 64) {
  151. output = output + String.fromCharCode(chr3);
  152. }
  153.  
  154. chr1 = chr2 = chr3 = "";
  155. enc1 = enc2 = enc3 = enc4 = "";
  156.  
  157. } while (i < input.length);
  158.  
  159. return output;
  160. }
  161. };
  162.  
  163. /* jshint ignore:end */
  164. });
  165.  
  166. 'use strict';
  167.  
  168. // declare modules
  169. angular.module('Authentication', []);
  170. angular.module('Home', []);
  171.  
  172. angular.module('BasicHttpAuthExample', [
  173. 'Authentication',
  174. 'Home',
  175. 'ngRoute',
  176. 'ngCookies'
  177. ])
  178.  
  179. .config(['$routeProvider', function ($routeProvider) {
  180.  
  181. $routeProvider
  182. .when('/login', {
  183. controller: 'LoginController',
  184. templateUrl: 'modules/authentication/views/login.html',
  185. hideMenus: true
  186. })
  187.  
  188. .when('/', {
  189. controller: 'HomeController',
  190. templateUrl: 'modules/home/views/home.html'
  191. })
  192.  
  193. .otherwise({ redirectTo: '/login' });
  194. }])
  195.  
  196. .run(['$rootScope', '$location', '$cookieStore', '$http',
  197. function ($rootScope, $location, $cookieStore, $http) {
  198. // keep user logged in after page refresh
  199. $rootScope.globals = $cookieStore.get('globals') || {};
  200. if ($rootScope.globals.currentUser) {
  201. $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
  202. }
  203.  
  204. $rootScope.$on('$locationChangeStart', function (event, next, current) {
  205. // redirect to login page if not logged in
  206. if ($location.path() !== '/login' && !$rootScope.globals.currentUser) {
  207. $location.path('/login');
  208. }
  209. });
  210. }]);
  211.  
  212. <script src="@routes.Assets.versioned("javascripts/app/controllers.js")" type="text/javascript"></script>
  213. <script src="@routes.Assets.versioned("javascripts/app/services.js")" type="text/javascript"></script>
  214. <script src="@routes.Assets.versioned("javascripts/app/app.js")" type="text/javascript"></script>
  215.  
  216. Use the function form of "use strict".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement