Guest User

Untitled

a guest
Dec 6th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import { Injectable, OnInit } from '@angular/core';
  2. import { HttpRequest, HttpResponse, HttpHandler, HttpEvent, HttpInterceptor, HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
  3. import { Observable, of, throwError } from 'rxjs';
  4.  
  5. import { User } from '@/data/models/property/user';
  6. import { UserService } from '@/_services/user.service';
  7.  
  8. @Injectable()
  9. export class FakeBackendInterceptor implements HttpInterceptor, OnInit {
  10.  
  11. users: User[];
  12.  
  13. constructor(private http: HttpClient, private userService: UserService) { }
  14.  
  15. ngOnInit() {
  16. this.getAll();
  17. }
  18. private getAll(): void {
  19. this.userService.getAll()
  20. .subscribe(data => this.users = data);
  21. }
  22.  
  23. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
  24. // authenticate
  25. if (request.url.endsWith('/users/authenticate') && request.method === 'POST') {
  26. // find if any user matches login credentials
  27. const filteredUsers = this.users.filter(user => {
  28. if (user.email === request.body.email && user.password === request.body.password) {
  29. return user;
  30. }
  31. });
  32.  
  33. if (filteredUsers) {
  34. // if login details are valid return 200 OK with user details and fake jwt token
  35. const user = filteredUsers[0];
  36. const body = {
  37. id: user.id,
  38. email: user.email,
  39. firstName: user.firstName,
  40. lastName: user.lastName,
  41. studentNo: user.studentNo,
  42. isStaff: user.isStaff,
  43. token: 'fake-jwt-token'
  44. };
  45.  
  46. return of(new HttpResponse({ status: 200, body: body }));
  47. } else {
  48. // else return 400 bad request
  49. return throwError({ error: { message: 'Email or password is incorrect' } });
  50. }
  51. }
  52. return next.handle(request);
  53.  
  54. }
  55. }
  56.  
  57. import { Injectable } from '@angular/core';
  58. import { HttpClient } from '@angular/common/http';
  59. import { Observable, of } from 'rxjs';
  60. import { catchError, map, tap } from 'rxjs/operators';
  61.  
  62. import { User } from '../data/models/property/user';
  63.  
  64. @Injectable({ providedIn: 'root' })
  65. export class UserService {
  66. constructor(private http: HttpClient) { }
  67.  
  68. getAll(): Observable<User[]> {
  69. return this.http.get<User[]>('api/users')
  70. .pipe(
  71. catchError(this.handleError('getAll', []))
  72. );
  73. }
  74.  
  75. getById(id: number) {
  76. return this.http.get(`/users/${id}`);
  77. }
  78.  
  79. register(user: User) {
  80. return this.http.post(`/users/register`, user);
  81. }
  82.  
  83. update(user: User) {
  84. return this.http.put(`/users/${user.id}`, user);
  85. }
  86.  
  87. delete(id: number) {
  88. return this.http.delete(`/users/${id}`);
  89. }
  90.  
  91. private handleError<T>(operation = 'operation', result ?: T) {
  92. return (error: any): Observable<T> => {
  93.  
  94. // TODO: send the error to remote logging infrastructure
  95. console.error(error.name); // log to console instead
  96.  
  97. // Let the app keep running by returning an empty result.
  98. return of(result as T);
  99. };
  100. }
  101. }
Add Comment
Please, Sign In to add comment