api-local.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { LoginRequest } from '../login/login-request';
@Injectable({
providedIn: 'root'
})
export class ApiLocalService {
constructor(private httpClient: HttpClient) { }
private headers = new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json, text/plain'
});
userLoginLocal(){
console.log("inside LOCAL API service");
return this.httpClient.get('../assets/data/login.json', {headers:this.headers});
}
getUserListLocal(){
return this.httpClient.get('../assets/data/userList.json' , { headers: this.headers })
}
}
api.service.ts
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { LoginRequest } from '../login/login-request';
import { EnvironmentConfig, ENV_CONFIG } from './environment-config.interface';
@Injectable({
providedIn: 'root'
})
export class ApiService {
public apiUrl: string;
constructor(@Inject(ENV_CONFIG) config: EnvironmentConfig,private httpClient: HttpClient,) {
this.apiUrl = `${config.environment.baseUrl}`;
}
private headers = new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json, text/plain'
});
userLoginLocal(){
console.log("inside LOCAL API service");
return this.httpClient.get('../assets/data/login.json', {headers:this.headers});
}
getUserListLocal(){
return this.httpClient.get('../assets/data/userList.json' , { headers: this.headers })
}
}
auth-service.ts
import { EventEmitter, Injectable, Output } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { LoginRequest } from '../login/login-request';
import { LoginResponse } from '../login/login-response';
import { LocalStorageService } from 'ngx-webstorage';
import { map, tap ,catchError} from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { ApiLocalService } from './api-local.service';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private API_URL = environment.API_URL;
// private Local_Env = environment.Local_Env;
private roles: Array<string> = [];
@Output() loggedIn: EventEmitter<boolean> = new EventEmitter();
@Output() username: EventEmitter<string> = new EventEmitter();
@Output() admin: EventEmitter<boolean> = new EventEmitter();
@Output() superAdmin: EventEmitter<boolean> = new EventEmitter();
@Output() client: EventEmitter<boolean> = new EventEmitter();
refreshTokenPayload = {
refreshToken: this.getRefreshToken(),
username: this.getUserName()
}
constructor(private httpClient: HttpClient,private localStorage: LocalStorageService,
private apiLocalService:ApiLocalService) {
}
adminLogin(loginRequest: LoginRequest): Observable<boolean> {
console.log("loginrequest:"+loginRequest);
return this.httpClient.post<any>(this.API_URL+'/auth-service/login', loginRequest)
.pipe(map(data => {
console.log(data)
if(data.roles.find((role: string) => role !='ROLE_SUPER_ADMIN' && role !='ROLE_ADMIN' && role !='ROLE_CLIENT')){
return false;
}else{
this.localStorage.store('authenticationToken', data.authenticationToken);
this.localStorage.store('username', data.username);
this.localStorage.store('refreshToken', data.refreshToken);
this.localStorage.store('expiresAt', data.expiresAt);
this.localStorage.store('roles', JSON.stringify(data.roles));
this.loggedIn.emit(true);
this.username.emit(data.username);
if(data.roles.find((role: string) => role =='ROLE_ADMIN')){
this.admin.emit(true);
}
if(data.roles.find((role: string) => role =='ROLE_SUPER_ADMIN')){
this.superAdmin.emit(true);
}
if(data.roles.find((role: string) => role =='ROLE_CLIENT')){
this.client.emit(true);
}
return true;
}
}));
}
Auth-service has the adminLogin which is called in 'login' component. My requirement is to call the api-local.service.ts when the environment is local, otherwise call api.service.ts when the environment is qa. These services should be injected in auth-service.ts inside the adminLogin(). Quick assistance would be highly appreciated...