Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {Headers, Http, Response} from '@angular/http';
  3. import { Observable } from 'rxjs/Observable';
  4. import 'rxjs/add/operator/map'
  5. import {AppSettings} from '../app.settings';
  6. import {HttpClient} from "../http-client";
  7. import {Router} from "@angular/router";
  8. import {User, City} from "../_models";
  9.  
  10. @Injectable()
  11. export class AuthService {
  12.  
  13. public user: User = new User();
  14.  
  15. constructor (
  16. private http: HttpClient,
  17. private router: Router,
  18. ) {
  19. this.profile().subscribe();
  20. }
  21.  
  22.  
  23. isAuth(): boolean {
  24. return this.http.isAuth();
  25. }
  26.  
  27. token():string {
  28. return this.http.token();
  29. }
  30.  
  31. login(email: string, password: string) {
  32. return this.http.post(AppSettings.API_URL+'auth/login', { email: email, password: password })
  33. .map((response: Response) => {
  34. console.log(response);
  35. let user = response.json();
  36. if (user && user.data.token) {
  37. localStorage.setItem('token', user.data.token);
  38. console.log("saved");
  39. }
  40. this.profile().subscribe();
  41. });
  42. }
  43.  
  44. register(email: string, password: string, name: string) {
  45. return this.http.post(AppSettings.API_URL+'auth/register', { email: email, password: password, name: name })
  46. .map((response: Response) => {
  47. console.log(response);
  48. let user = response.json();
  49. if (user && user.data.token) {
  50. localStorage.setItem('token', user.data.token);
  51. console.log("saved");
  52. }
  53. this.profile().subscribe();
  54. });
  55. }
  56.  
  57. logout() {
  58. localStorage.removeItem('token');
  59. this.http.post(AppSettings.API_URL+'auth/logout', { })
  60. .map((response: Response) => {})
  61. .subscribe();
  62. this.user = null;
  63. this.router.navigate(['/home']);
  64. }
  65.  
  66. update(email, name: string, city: City) {
  67. let data = {};
  68. if (email != null && name != null) {
  69. data = {
  70. email: email,
  71. name: name
  72. };
  73. this.user.email = email;
  74. this.user.name = name;
  75. } else {
  76. data = {
  77. city: city.id
  78. };
  79. }
  80.  
  81. return this.http.put(AppSettings.API_URL+'auth/update', data)
  82. .map((response: Response) => {
  83. console.log(response);
  84. });
  85. }
  86.  
  87. update_password(oldPassword:string, newPassword:string) {
  88. return this.http.put(AppSettings.API_URL+'auth/update_password',
  89. {
  90. old_password: oldPassword,
  91. password: newPassword,
  92. rePassword: newPassword
  93. });
  94. }
  95.  
  96. profile() {
  97. return this.http.get(AppSettings.API_URL+'auth/profile')
  98. .map((response: Response) => {
  99. let res = response.json();
  100. if (res.status == "success") {
  101. console.log(res.data.user);
  102. this.user = new User(res.data.user);
  103. }
  104. });
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement