### File

`src/app/core/user.service.ts`

### Index

###### Methods
Public getCurrentUser Protected getHeaders Public login

### Constructor

`constructor(router: Router, http: HttpClient)`
Defined in src/app/core/user.service.ts:10
Parameters :
Name | Type | Optional
router | `Router` | No
http | `HttpClient` | No

### Methods

Public getCurrentUser
`getCurrentUser()`
Defined in src/app/core/user.service.ts:13
Returns : `Observable<>`

Protected getHeaders
`getHeaders()`
Defined in src/app/core/user.service.ts:29
Returns : `any`

Public login
`login(username: string, password: string)`
Defined in src/app/core/user.service.ts:19
Parameters :
Name | Type | Optional
username | `string` | No
password | `string` | No
Returns : `Observable<any>`

    import { catchError } from 'rxjs/operators';
    import { Injectable } from '@angular/core';
    import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http';
    import { Router } from '@angular/router';
    import { Observable } from 'rxjs';

    import { Settings } from './settings';

    @Injectable()
    export class UserService {
      constructor(protected router: Router, private http: HttpClient) {}

      public getCurrentUser(): Observable<unknown> {
        return this.http
          .get(`${Settings.userAPI}/current`, { headers: this.getHeaders() })
          .pipe(catchError((_) => _));
      }

      public login(username: string, password: string): Observable<any> {
        const url = `${Settings.userAPI}/login`;
        const body = { username, password };
        const options = {
          headers: this.getHeaders(),
          observe: 'response' as 'body',
        };
        return this.http.post<any>(url, body, options);
      }

      protected getHeaders() {
        const headers = new HttpHeaders();
        headers.append('Accept', 'application/json');
        headers.append('Content-Type', 'application/json');
        return headers;
      }
    }

# results matching ""

# No results matching ""