Advertisement
Guest User

login

a guest
Jan 18th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. class loginService {
  5. /*@ngInject*/
  6. constructor($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages) {
  7. this.BASE_URL = BASE_URL;
  8. this.REST_PATH = REST_PATH;
  9. this.$q = $q;
  10. this.$resource = $resource;
  11. this.LOGIN_PATH = LOGIN_PATH;
  12. this.LOGOUT_PATH = LOGOUT_PATH;
  13. this.IS_LOGGED_IN_PATH = IS_LOGGED_IN_PATH;
  14. this.$httpParamSerializerJQLike = $httpParamSerializerJQLike;
  15. this.messages = messages;
  16. this.state = {};
  17. this.userStatus = { authorized: false };
  18. }
  19.  
  20.  
  21. initLoginResource() {
  22. const { $resource, BASE_URL, REST_PATH, LOGIN_PATH, $httpParamSerializerJQLike } = this;
  23. return $resource(BASE_URL + REST_PATH + LOGIN_PATH, null, {
  24. doLogin: {
  25. method: 'POST',
  26. params: {},
  27. transformRequest: (data) =>{
  28. return $httpParamSerializerJQLike(data);
  29. },
  30. transformResponse: (data, headersGetter, status)=> {
  31. return {data: data, status: status};
  32. },
  33. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  34. }
  35. });
  36. }
  37.  
  38. initIsLoggedInResource() {
  39. const { $resource, BASE_URL, REST_PATH , IS_LOGGED_IN_PATH } = this;
  40. return $resource(BASE_URL + REST_PATH + IS_LOGGED_IN_PATH, null, {
  41. isLoggedIn: {
  42. method: 'GET'
  43. }
  44. });
  45. }
  46.  
  47. initLogoutResource() {
  48. const { $resource, BASE_URL, REST_PATH , LOGOUT_PATH } = this;
  49. return $resource(BASE_URL + REST_PATH + LOGOUT_PATH, null, {
  50. doLogout: {
  51. method: 'POST'
  52. }
  53. });
  54. }
  55.  
  56. isUserAuthorized() {
  57. return this.$q((resolve, reject) => {
  58. this.initIsLoggedInResource().isLoggedIn().$promise
  59. .then( (data)=> {
  60. this.userStatus.authorized = true;
  61. resolve(true);
  62. }, (data) => {
  63. this.userStatus.authorized = false;
  64. reject(false);
  65. });
  66. });
  67. }
  68.  
  69. performLogin(name, password) {
  70. return this.$q( (resolve, reject) => {
  71. this.initLoginResource().doLogin({}, {
  72. username: name,
  73. password: password
  74. }).$promise.then( (data) => {
  75. this.userStatus.authorized = true;
  76. return resolve(data);
  77. }, (data) => {
  78. this.userStatus.authorized = false;
  79. return reject(data.data);
  80. });
  81. });
  82. }
  83.  
  84. logOut() {
  85. return this.$q( (resolve, reject) => {
  86. this.initLogoutResource().doLogout().$promise
  87. .then( (data) => {
  88. this.userStatus.authorized = false;
  89. return resolve(data);
  90. }, (data) => {
  91. this.userStatus.authorized = false;
  92. return resolve(data); // resolve request anyway
  93. });
  94. });
  95. }
  96.  
  97. }
  98.  
  99. angular.module('app.core')
  100. .factory('loginService',
  101. ['$resource', '$q', 'BASE_URL', 'REST_PATH', 'LOGIN_PATH', 'LOGOUT_PATH', 'IS_LOGGED_IN_PATH', '$httpParamSerializerJQLike', 'messages',
  102. ($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages) =>
  103. new loginService($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages)
  104. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement