Reputation: 3949
Oke,
I have an Angular 8 application and I have an HttpMaintenanceInterceptor and I am not using cookies for it. but a:
getAccessToken method in the authService, like this:
getAccessToken(): string {
return this._user ? this._user.access_token : null;
}
and I have a get method. A get method looks like this:
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
But the problem now is that the the propertie:
patientUUID
is always null. OR this is the output of it:
http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/type/physical"
So I try to sent the patientUUID in the HttpMaintenanceInterceptor.
The HttpMaintenanceInterceptor looks like this:
export class HttpMaintenanceInterceptor implements HttpInterceptor {
needsAuthenticatedUser = true;
route: string;
constructor(private auth: AuthService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const accessToken = this.auth.getAccessToken();
if (accessToken != null) {
console.log(accessToken);
const duplicate = request.clone({
setHeaders: { Authorization: `Bearer ${accessToken}` }
});
const user$ = this.auth.loginStatus()
.pipe( take( 1 ) );
user$.pipe(
map( user => {
console.log('hello there nice to see you!!');
let parsedRoute = this.route;
if ( this.needsAuthenticatedUser ) {
if ( !user ) {
throw Error( 'Tried to call api that requires login without a user profile present' );
} else {
parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
// console.log('User Sub ' + user.profile.sub);
console.log('User participant ' + user.profile.participant);
parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
}
}
return environment.ApiOrigin + parsedRoute;
} ),
);
return next.handle(duplicate);
} else {
return next.handle(request);
}
}
}
But I dont get the patientUUID.
But I get the accessToken: console.log(accessToken); looks like this:
mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....
So my question is how to pass the patientUUID? so that in the api requests the patientUUID will not be null anymore.
Thank you
oke, I changed to this:
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>(`/api/patient/{patientUUID}/DossierEntry/` + entryType);
}
but that is not the problem I think.
Because the problem is this:
console.log('hello there nice to see you!!');
It doesn't reach that line.
Upvotes: 2
Views: 1190
Reputation: 18381
Backquote should be used instead of simple quote
'/api/patient/${patientUUID}/DossierEntry/'
should rather be
`/api/patient/${patientUUID}/DossierEntry/`
The same thing holds when using parsedRoute.replace
const user$ = this.auth.loginStatus()
.pipe( take( 1 ) );
user$.pipe(
map( user => {
console.log('hello there nice to see you!!');
let parsedRoute = this.route;
if ( this.needsAuthenticatedUser ) {
if ( !user ) {
throw Error( 'Tried to call api that requires login without a user profile present' );
} else {
parsedRoute = parsedRoute.replace('{userId}', user.profile.sub);
// console.log('User Sub ' + user.profile.sub);
console.log('User participant ' + user.profile.participant);
parsedRoute = parsedRoute.replace('{patientUUID}', user.profile.participant);
}
}
return environment.ApiOrigin + parsedRoute;
} ),
);
This part of the code will never be executed because you're not subscribing to the observable. That is why the value of console.log
is never printed to the console
Upvotes: 2