Merge pull request #32084 from votdev/auth_service_observable

mgr/dashboard: Use Observable in auth.service

Reviewed-by: Alfonso Martínez <almartin@redhat.com>
This commit is contained in:
Lenz Grimmer 2019-12-09 16:34:50 +00:00 committed by GitHub
commit 9960f9365a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 8 deletions

View File

@ -62,7 +62,7 @@ export class LoginComponent implements OnInit {
}
login() {
this.authService.login(this.model).then(() => {
this.authService.login(this.model).subscribe(() => {
this.router.navigate(['']);
});
}

View File

@ -34,7 +34,7 @@ describe('AuthService', () => {
it('should login and save the user', fakeAsync(() => {
const fakeCredentials = { username: 'foo', password: 'bar' };
const fakeResponse = { username: 'foo', token: 'tokenbytes' };
service.login(<any>fakeCredentials);
service.login(fakeCredentials).subscribe();
const req = httpTesting.expectOne('api/auth');
expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(fakeCredentials);

View File

@ -2,6 +2,9 @@ import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Credentials } from '../models/credentials';
import { LoginResponse } from '../models/login-response';
import { AuthStorageService } from '../services/auth-storage.service';
@ -21,13 +24,12 @@ export class AuthService {
return this.http.post('api/auth/check', { token: token });
}
login(credentials: Credentials) {
return this.http
.post('api/auth', credentials)
.toPromise()
.then((resp: LoginResponse) => {
login(credentials: Credentials): Observable<LoginResponse> {
return this.http.post('api/auth', credentials).pipe(
tap((resp: LoginResponse) => {
this.authStorageService.set(resp.username, resp.token, resp.permissions, resp.sso);
});
})
);
}
logout(callback: Function = null) {