我最近将我的应用程序从Angular2迁移到Angular6
我的构建失败了,因为Angular2-JWT,所以我将它升级到@auth0/angular-jwt。
现在,当AuthHTTP和AuthConfig被废弃时,我在更新我的代码时陷入了困境。
我的security.module.ts有一个security.module.ts。我得把一个新的AuthHttp还给我的security.module.ts。
export function AuthHttpServiceFactory( http: Http, options: RequestOptions) {
const accessToken = this.accessTokenService.getAccessToken();
const userInfoToken = this.accessTokenService.getUserInfoToken();
return new AuthHttp(
new AuthConfig({
tokenName: 'token',
tokenGetter: (() => accessToken),
globalHeaders: [{ 'Content-Type': 'application/json' }, { 'userinfotoken': userInfoToken }],
}), http, options);
}现在,我在返回AuthHttp时出错。
我的Security.Module.ts如下。
@NgModule({
providers: [
LocalStorageService,
{
provide: AuthHttp,
useFactory: AuthHttpServiceFactory,
deps: [Http, RequestOptions]
}
]
})我在提供: AuthHttp也有错误。
任何帮助都是非常感谢的。
发布于 2018-11-12 19:53:14
这是如何使用HttpClient和HttpHeaders作为服务添加身份验证。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class VehicleService {
headers;
private readonly vehicleEndpoint = 'api/vehicles';
constructor(private http: HttpClient) {
}
create(vehicle) {
return this.http.post(this.vehicleEndpoint, vehicle, this.addHeaders());
}
addHeaders() {
var access_token = localStorage.getItem('access_token');
return { headers: new HttpHeaders().set("Authorization", `Bearer
${access_token}`) };
}access_token是Json令牌。
https://stackoverflow.com/questions/53002364
复制相似问题