Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. export class UserService {
  2.  
  3. /**
  4. * Validate user's password - returns true if validated
  5. * Used in register and updatePassword functions
  6. */
  7. passwordValidation(pass: string) : boolean {
  8. if (pass.length >= 8 && /\d/.test(pass) && /[a-z]/.test(pass) && /[A-Z]/.test(pass))
  9. return true;
  10. else return false;
  11. }
  12.  
  13. /**
  14. * Represents observer currently of logged user
  15. */
  16. loggedUser: Subject<User>;
  17.  
  18. /**
  19. * Http headers and options used to create http requests
  20. */
  21. headers: Headers;
  22. options: RequestOptions;
  23.  
  24. constructor(private http: Http) {
  25.  
  26. this.loggedUser = new Subject<User>();
  27.  
  28. this.headers = new Headers({'Content-Type': 'application/json'});
  29. this.options = new RequestOptions({headers: this.headers});
  30. }
  31.  
  32. /**
  33. * Logs in existing user into selfiemap app
  34. * @param username Name of user
  35. * @param password Password of user
  36. */
  37. login(username: string, password: string) {
  38.  
  39. let body = JSON.stringify({username, password});
  40.  
  41. this.http.post(API_USER_LOGIN, body, this.options)
  42. .map(data => data.json()).subscribe((user) => {
  43.  
  44. this.loggedUser.next(user);
  45. })
  46. }
  47.  
  48. /**
  49. * Registers new user into selfiemap app
  50. * @param username Name of user
  51. * @param password Password of user
  52. * @param email E-mail of user
  53. * @param birthDate Birth date of user
  54. */
  55. register(username: string, password: string, email: string, birthDate: Date) {
  56. if (this.passwordValidation(password)) {
  57. let body = JSON.stringify({username, password, email, birthDate});
  58.  
  59. this.http.post(API_USER_REGISTER, body, this.options)
  60. .map(data => data.json()).subscribe((user) => {
  61.  
  62. this.loggedUser.next(user);
  63. });
  64. }
  65. else console.log('Bad password')
  66. }
  67.  
  68. /**
  69. * Update password of existing user
  70. * @param oldPassword Old password to verify ownership of account
  71. * @param newPassword New password
  72. */
  73. updatePassword(oldPassword: string, newPassword: string) {
  74. if (this.passwordValidation(newPassword)) {
  75. let body = JSON.stringify({oldPassword, newPassword});
  76.  
  77. this.http.put(API_USER_CHANGEPASSWORD, body, this.options)
  78. .map(data => data.json()).subscribe((user) => {
  79.  
  80. this.loggedUser.next(user);
  81. });
  82. }
  83. else console.log('Bad password');
  84. }
  85.  
  86. /**
  87. * Change user's language of seliemap app
  88. * @param language Language id (sk,cz,en,...)
  89. */
  90. setLanguage(language: string) {
  91.  
  92. let body = JSON.stringify({language});
  93. this.http.put(API_USER_LANGUAGE, body, this.options)
  94. .map(data => data.json()).subscribe((user) => {
  95.  
  96. this.loggedUser.next(user);
  97. });
  98. }
  99.  
  100. /**
  101. * Change privacy of user's account
  102. * @param privateProfile If true, user's profile is private, if false, user's profile is public
  103. */
  104. setPrivacy(privateProfile: boolean) {
  105.  
  106. let body = JSON.stringify({privateProfile});
  107. this.http.put(API_USER_PRIVACY, body, this.options)
  108. .map(data => data.json()).subscribe((user) => {
  109.  
  110. this.loggedUser.next(user);
  111. });
  112. }
  113.  
  114. /**
  115. * Get last known location of user
  116. * @returns {string} Last location
  117. */
  118. getLastLocation() {
  119.  
  120. return '';
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement