angular2-jwt No provider for AuthConfig
Asked Answered
M

4

11

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!
Marquardt answered 2/9, 2016 at 13:49 Comment(1)
Maybe this is helpful: #42363537Delicacy
H
18

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
        })
    ],
Heterophyte answered 2/9, 2016 at 14:55 Comment(0)
D
0

You have to provide all injected dependencies in your NgModule. add AuthConfig to your @NgModule's declarations.

edit: This might be nonsense.

Have you updated angular2-jwt to the current version?

Duran answered 2/9, 2016 at 14:13 Comment(1)
I get this error when I add AuthConfig to providers in NgModule. I am using "angular2-jwt": "0.1.21" --error -> Error: Error: Can't resolve all parameters for AuthConfig:Marquardt
S
0

This error can also occur if you have the providers in the wrong order:

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

caused the error with Angular 2.0.0 and angular2-jwt 0.1.23.

Stepheniestephens answered 27/9, 2016 at 9:5 Comment(0)
Z
0

Try this: ...

 imports: [ AuthModule.forRoot(new AuthConfig({
  headerName: 'Authorization',
  headerPrefix: 'my-bearer-prefix',
  tokenName: 'my-token-name',
  tokenGetter: (() => localStorage.getItem('token') || ''),
  globalHeaders: [{ 'Content-Type': 'application/json' }],
  noJwtError: true
}))]
Zizith answered 12/1, 2018 at 22:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.