Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. .factory('AuthenticationService',
  2. ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
  3. function (Base64, $http, $cookieStore, $rootScope, $timeout) {
  4. var service = {};
  5.  
  6. service.Login = function (UserName, Password, callback) {
  7.  
  8. /* Dummy authentication for testing, uses $timeout to simulate api call
  9. ----------------------------------------------
  10. $timeout(function () {
  11. var response = { success: UserName === 'test' && Password === 'test' };
  12. if (!response.success) {
  13. response.message = 'UserName or Password is incorrect';
  14. }
  15. callback(response);
  16. }, 1000);
  17.  
  18. */
  19.  
  20.  
  21. /* Use this for real authentication
  22. ----------------------------------------------*/
  23. $http({
  24. method: 'POST',
  25. url: 'http://216.0.1.209/AuthenticateUser',
  26. data: {
  27. UserName: 'UserName',
  28. Password: 'Password'
  29. },
  30. headers: {
  31. 'content-type: multipart/form-data; boundary=---011000010111000001101001'
  32. }
  33. }).then(function(response) {
  34. console.log(response)
  35. });
  36.  
  37. };
  38.  
  39. service.SetCredentials = function (UserName, Password) {
  40. var authdata = Base64.encode(UserName + ':' + Password);
  41.  
  42. $rootScope.globals = {
  43. currentUser: {
  44. UserName: UserName,
  45. authdata: authdata
  46. }
  47. };
  48.  
  49. $http.defaults.headers.common['Authorization'] = '0 ' + authdata; // jshint ignore:line
  50. $cookieStore.put('globals', $rootScope.globals);
  51. };
  52.  
  53. service.ClearCredentials = function () {
  54. $rootScope.globals = {};
  55. $cookieStore.remove('globals');
  56. $http.defaults.headers.common.Authorization = '0 ';
  57. };
  58.  
  59. return service;
  60. }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement