Guest User

Untitled

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