Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /**
  2. * Created by vvn050 on 08.04.16.
  3. */
  4. (function () {
  5. 'use strict';
  6.  
  7. angular
  8. .module('portal.client')
  9. .factory('loginService', ['$http', '$q', 'CONSTANTS', 'authenticationData','$location', function ($http, $q, CONSTANTS, authenticationData,$location) {
  10. var loginService = this,
  11. tokenEndPoint = CONSTANTS.BASE + CONSTANTS.TOKEN,
  12. deferred,
  13. userInfo;
  14.  
  15. loginService.login = function (username, password, rememberMeChecked) {
  16. deferred = $q.defer();
  17.  
  18. var data = "grant_type=password&username=" + username + "&password=" + password;
  19.  
  20. $http.post(tokenEndPoint, data, {
  21. headers: {
  22. 'Content-Type': 'application/x-www-form-urlencoded'
  23. }
  24. }).success(function (response) {
  25. console.log(response);
  26. console.log('hoiata11111111111111');
  27. userInfo = {
  28. access_token: response.access_token,
  29. refresh_token: response.refresh_token,
  30. username: response.sub,
  31. expires_in: response.expires_in
  32. };
  33.  
  34. if (rememberMeChecked) {
  35. authenticationData.setLocalStorage(userInfo);
  36. authenticationData.userInfo.storageInUse = 'localStorage';
  37. }
  38. else {
  39. authenticationData.setSessionStorage(userInfo);
  40. authenticationData.userInfo.storageInUse = 'sessionStorage';
  41. }
  42.  
  43. authenticationData.userInfo.clientIsAuthenticated = true;
  44. authenticationData.userInfo.clientUsername = response.username;
  45. $location.path('dashboard');
  46. //TODO send user to a state
  47. // $state.go('index.');
  48. deferred.resolve(null);
  49.  
  50. })
  51. .error(function (err) {
  52. console.log(err);
  53. authenticationData.userInfo.clientIsAuthenticated = false;
  54. authenticationData.userInfo.clientUsername = '';
  55. deferred.resolve(err);
  56. });
  57. return deferred.promise;
  58. };
  59.  
  60. return this;
  61.  
  62. }]);
  63. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement