All files / src/app/core/services auth.service.ts

79.16% Statements 19/24
100% Branches 2/2
76.92% Functions 10/13
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 439x   9x 9x     9x 9x         9x   23x   23x     4x   4x           7x   7x           4x       4x      
import { Injectable } from '@angular/core';
import { RegistrationRequest } from '@core/model/registration-request.interface';
import { Observable, switchMap } from 'rxjs';
import { DataService } from './data.service';
import { LoginRequest } from '@core/model/login-request.interface';
import { SessionInformation } from '@core/model/session-information.interface';
import { HttpHeaders } from '@angular/common/http';
import { CaptchaService } from './captcha.service';
 
@Injectable({
  providedIn: 'root'
})
export class AuthService {
 
  private apiPath = 'auth';
 
  constructor(private dataService: DataService, private captchaService: CaptchaService) { }
 
  public register(registrationRequest: RegistrationRequest): Observable<null> {
    return this.captchaService.getCaptchaToken().pipe(
      switchMap((token: string) => {
        return this.dataService.post<null>(`${this.apiPath}/register`, registrationRequest, {headers: new HttpHeaders().set('Captcha-Response', token)});
      })
    );
  }
 
  public login(loginRequest: LoginRequest): Observable<SessionInformation> {
    return this.captchaService.getCaptchaToken().pipe(
      switchMap((token: string) => {
        return this.dataService.post<SessionInformation>(`${this.apiPath}/login`, loginRequest, {headers: new HttpHeaders().set('Captcha-Response', token)});
      })
    );
  }
 
  public logout(): Observable<void> {
    return this.dataService.post<void>(`${this.apiPath}/logout`, null);
  }
  
  public refreshToken(): Observable<SessionInformation> {
    return this.dataService.post<SessionInformation>(`${this.apiPath}/refresh-token`, null);
  }
}