angularangular2-jwt

angular2-jwt No provider for AuthConfig


I am struggling with the angular2-jwt documentation for rc5

Here is my NgModule

import { AuthHttp } from 'angular2-jwt';



 @NgModule({
  imports: [ BrowserModule, 
             routing,
             HttpModule,
             FormsModule,

            ],
  declarations: [ 
                  AppComponent,
                  LoginComponent, 
                  NavbarComponent,
                  DashboardComponent,
                  ModelsComponent
                ],
  providers: [AuthGuard,
              ModelService,
              AuthHttp
              ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

Here Is my service

import { Injectable } from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';
import { Model } from './model';
import { AuthHttp } from 'angular2-jwt';
import {Observable} from "rxjs/Rx";

@Injectable()
export class ModelService {
  private _url = "http://127.0.0.1:8050/test/model";

  constructor(private _http: Http,private _authHttp: AuthHttp){
    //this.jwt = localStorage.getItem('id_token');
  }


  getPollModles(){
    return Observable.interval(5000).switchMap(() => this._authHttp.get(this._url)).map(res => res.json());
  }

}

How do I get angular2_jwt working with rc5 ?

When I add my service to the construtor I get the below erorr.

constructor(private route: ActivatedRoute,
                public router: Router,
                private modelService: ModelService) 

core.umd.js:5995EXCEPTION: Uncaught (in promise): Error: Error in ./ModelsComponent class ModelsComponent_Host - inline template:0:0 caused by: No provider for AuthConfig!

Solution

  • app.module Import like this:

    import { AuthHttp, AuthConfig, AUTH_PROVIDERS, provideAuth } from 'angular2-jwt';
    

    And add providers:

    providers: [        
            AuthHttp,
            provideAuth({
                headerName: 'Authorization',
                headerPrefix: 'bearer',
                tokenName: 'token',
                tokenGetter: (() => localStorage.getItem('id_token')),
                globalHeaders: [{ 'Content-Type': 'application/json' }],
                noJwtError: true
            })
        ],