fix(self-service): update API to leverage current HTTP interceptors (#124)

Update API for self service user management to leverage current HTTP interceptors for setting the API prefix and request headers.

Fixes #123
This commit is contained in:
Abhay Chawla
2018-07-14 21:14:55 +05:30
committed by GitHub
parent d9c35b8759
commit a57846b88b
3 changed files with 23 additions and 44 deletions
@@ -92,16 +92,13 @@ export class AuthenticationService {
this.storage = this.rememberMe ? localStorage : sessionStorage;
if (environment.oauth.enabled) {
return this.http.disableApiPrefix().post(
`${environment.oauth.serverUrl}
/oauth/token?username=${loginContext.username}&password=${loginContext.password}
&client_id=community-app&grant_type=password&client_secret=123`, {}
).pipe(
map((tokenResponse: OAuth2Token) => {
this.getUserDetails(tokenResponse);
return of(true);
})
);
return this.http.disableApiPrefix().post(`${environment.oauth.serverUrl}/oauth/token?username=${loginContext.username}&password=${loginContext.password}&client_id=community-app&grant_type=password&client_secret=123`, {})
.pipe(
map((tokenResponse: OAuth2Token) => {
this.getUserDetails(tokenResponse);
return of(true);
})
);
} else {
return this.http.post(`/authentication?username=${loginContext.username}&password=${loginContext.password}`, {})
.pipe(
@@ -131,11 +128,8 @@ export class AuthenticationService {
refreshOAuthAccessToken() {
const oAuthRefreshToken = JSON.parse(this.storage.getItem(oAuthTokenDetailsStorageKey)).refresh_token;
this.authenticationInterceptor.removeAuthorization();
this.http.disableApiPrefix().post(
`${environment.oauth.serverUrl}
/oauth/token?client_id=community-app&grant_type=refresh_token&client_secret=123
&refresh_token=${oAuthRefreshToken}`, {}
).subscribe((tokenResponse: OAuth2Token) => {
this.http.disableApiPrefix().post(`${environment.oauth.serverUrl}/oauth/token?client_id=community-app&grant_type=refresh_token&client_secret=123&refresh_token=${oAuthRefreshToken}`, {})
.subscribe((tokenResponse: OAuth2Token) => {
this.storage.setItem(oAuthTokenDetailsStorageKey, JSON.stringify(tokenResponse));
this.authenticationInterceptor.setAuthorizationToken(tokenResponse.access_token);
this.refreshTokenOnExpiry(tokenResponse.expires_in);
+13 -28
View File
@@ -1,21 +1,8 @@
import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { map } from 'rxjs/internal/operators/map';
const username = 'mifos';
const password = 'password';
const token = btoa(`${username}:${password}`);
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': `Basic ${token}`,
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Origin': '*'
})
};
@Injectable({
providedIn: 'root'
})
@@ -24,20 +11,18 @@ export class UserService {
constructor(private http: HttpClient) { }
getUsers(): Observable<any> {
return this.http.get<any>(
`https://demo.openmf.org/fineract-provider/api/v1/users?
fields=id,firstname,lastname,email,officeName,staff&tenantIdentifier=default&pretty=true`,
httpOptions).pipe(
map((res) => {
res.forEach((user: any) => {
user.name = `${user.firstname} ${user.lastname}`;
user.staff = user.staff ? `${user.staff.firstname} ${user.staff.lastname}` : '';
delete user.firstname;
delete user.lastname;
});
return res;
})
);
return this.http.get<any>(`/users?fields=id,firstname,lastname,email,officeName,staff&tenantIdentifier=default&pretty=true`)
.pipe(
map((res) => {
res.forEach((user: any) => {
user.name = `${user.firstname} ${user.lastname}`;
user.staff = user.staff ? `${user.staff.firstname} ${user.staff.lastname}` : '';
delete user.firstname;
delete user.lastname;
});
return res;
})
);
}
}
+1 -1
View File
@@ -30,7 +30,7 @@
"label-position": true,
"max-line-length": [
true,
135
250
],
"member-access": false,
"member-ordering": [