· 5 years ago · Mar 18, 2020, 09:28 PM
1import { Injectable } from '@angular/core';
2import { HttpClient, HttpParams } from '@angular/common/http';
3import { environment } from 'src/environments/environment';
4import { Observable } from 'rxjs';
5
6@Injectable({
7 providedIn: 'root'
8})
9export class LoginService {
10
11 readonly loginURL = environment.apiBaseUrl + 'oauth/token';
12
13 constructor(private http: HttpClient) { }
14
15 public login(username: string, password: string): Observable<object> {
16 console.log(username + '::' + password);
17
18 const headers = {
19 'Authorization': 'Basic ' + btoa('my-client:my-secret'),
20 'Content-Type': 'application/x-www-form-urlencoded'
21 };
22
23 const body = new HttpParams()
24 .set('username', username)
25 .set('password', password)
26 .set('grant_type', 'password');
27
28 return this.http.post(this.loginURL, body, { headers });
29 }
30}