Mahmoud Abas
Mahmoud Abas

Reputation: 71

problem in using angular2-jwt and AuthHttp when upgrading angular from 4 to 6

I'm trying to update an angular4 application to angular6 and I was using angular2-jwt to send authentication to the web api,but in angular6 I gut this error: TypeError: Observable_1.Observable.defer is not a function see the error here

this is where I'm creating the token

 login(model: any) {
    const headers = new Headers({ 'Content-type': 'application/json' });
    const options = new RequestOptions({ headers: headers });
    return this.http
      .post(this.baseUrl, model, options)
      .map((response: Response) => {
        const user = response.json();
        if (user) {
          localStorage.setItem('token', user.userModel.tokenString);
          this.decodedToken = this.jwtHelper.decodeToken(user.userModel.tokenString);
          this.userToken = user.userModel.tokenString;
          this.currentUserModel = user.userModel;
          this.changeuserPhotoUrl(this.currentUserModel.userImage);
        }
      }).catch(this.handelError);
  }

this is my HttpServiceFactory

import { Http, RequestOptions } from "@angular/http";
import { AuthHttp, AuthConfig } from "angular2-jwt";
import { NgModule } from "@angular/core";

export function authenticationHttpServiceFactory(http: Http, options: RequestOptions) {
  return new AuthHttp(new AuthConfig({
    tokenName: 'token',
    tokenGetter: (() => localStorage.getItem('token')),
    globalHeaders: [{ 'Content-Type':'application/json'}]
  }),http,options);
}

@NgModule({
  providers: [
    {
      provide: AuthHttp,
      useFactory: authenticationHttpServiceFactory,
      deps: [Http, RequestOptions]
    }
    ]
})

export class AuthenticationModule { }

Upvotes: 0

Views: 335

Answers (1)

caglarturkurka
caglarturkurka

Reputation: 116

I think that you can use Interceptor to achieve your purpose. For example ;

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
 .......
 .....
}

and then you should put it into your module.ts like

{
  provide: HTTP_INTERCEPTORS,
  useClass: AuthInterceptor,
  multi: true
}

Upvotes: 1

Related Questions