Angular 如何在拦截器中传递userid
Oke 我有一个Angular 8应用程序和一个HttpMaintenance拦截器,我没有使用cookies。但是a: authService中的getAccessToken方法,如下所示:Angular 如何在拦截器中传递userid,angular,typescript,rest,restful-authentication,angular-http-interceptors,Angular,Typescript,Rest,Restful Authentication,Angular Http Interceptors,Oke 我有一个Angular 8应用程序和一个HttpMaintenance拦截器,我没有使用cookies。但是a: authService中的getAccessToken方法,如下所示: getAccessToken(): string { return this._user ? this._user.access_token : null; } getDossierEntry(type: String = '' ): Observable<DossierEntry
getAccessToken(): string {
return this._user ? this._user.access_token : null;
}
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
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);
}
}
}
mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....
我有一个get方法。get方法如下所示:
getAccessToken(): string {
return this._user ? this._user.access_token : null;
}
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
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);
}
}
}
mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....
始终为空。或者这是它的输出:
http://localhost:4200/api/patient/$%7BpatientUUID%7D/DossierEntry/type/physical"
所以我尝试在httpMaintenance拦截器中发送patientUUID
HttpMaintenance拦截器如下所示:
getAccessToken(): string {
return this._user ? this._user.access_token : null;
}
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>('/api/patient/${patientUUID}/DossierEntry/' + entryType);
}
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);
}
}
}
mIwM2U2MGNhMzgwYzczMzA2NjIcHM6Ly9k.....
所以我的问题是如何传递patientUUID?因此,在api请求中,patientUUID将不再为空
多谢各位
好的,我改成这样:
getDossierEntry(type: String = '' ): Observable<DossierEntry[]> {
const entryType = type === '' ? 'all' : 'type/' + type;
return this.http.get<DossierEntry[]>(`/api/patient/{patientUUID}/DossierEntry/` + entryType);
}
getDossierEntry(类型:String=''):可观察{
const entryType=type=='''all':'type/'+type;
返回这个.http.get(`/api/patient/{patientUUID}/DossierEntry/`+entryType);
}
但我认为这不是问题所在
因为问题是:
log('你好,很高兴见到你!!')
它没有达到那一行。应该使用反引号,而不是简单的引号 “/api/patient/${patientUUID}/docsierentry/” 宁可
`/api/patient/${patientUUID}/DossierEntry/`
使用parsedulote.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;
} ),
);
这部分代码将永远不会执行,因为您没有订阅可观察的。这就是为什么console.log
的值从来没有打印到控制台,而控制台是正确的api调用:getDossierEntry(type:String=''):Observable{const entryType=type===''?'all':'type/'+type;返回这个.http.get(/api/patient/{patientUUID}/DossierEntry/
+entryType);}看到我的答案了吗,查询模板中有一个$
:`/api/patient/${patientUUID}/DossierEntry/``但是它必须没有'$',我发布了正确的api调用。这是我的错误。因为没有属性:patientUUID,所以正确的调用是:/api/patient/{patientUUID}/DossierEntry/
您的问题不清楚。您希望请求中有UUID值,对吗?然后,在js中就是这样做的。请参见如何在js模板中嵌入表达式