web-dev-qa-db-fra.com

Angular 6 utilisant angular2-jwt

Après la migration de mon application de Angular 4 ^ à Angular 6 (version la plus récente), une erreur très étrange s'est produite dans mon service Auth.

Spécifique, le angular2-jwt package provoque une erreur mortelle lorsque j'essaie de "serve" ou "build" l'application dans l'environnement production. D'autre part, le même code fonctionne parfaitement sur l'environnement "dev".

OS -> MacOS 10.13.6

L'erreur que j'ai eu quand execute ng serve --configuration production:

ERROR in : Error: Internal error: unknown identifier [{"filePath":"/Users/paulo/Projects/eleo-usuario/node_modules/angular2-jwt/angular2-jwt.d.ts","name":"AuthHttp","members":[]},{"filePath":"/Users/paulo/Projects/eleo-usuario/node_modules/@angular/common/common.d.ts","name":"LocationStrategy","members":[]}]
at Object.importExpr$$1 [as importExpr] (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21757:27)
at tokenExpr (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:11810:43)
at providerDef (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:11736:24)
at /Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:11906:81
at Array.map (<anonymous>)
at NgModuleCompiler.compile (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:11906:48)
at AotCompiler._compileModule (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21702:36)
at /Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21621:70
at Array.forEach (<anonymous>)
at AotCompiler._compileImplFile (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21621:23)
at /Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21611:74
at Array.map (<anonymous>)
at AotCompiler.emitAllImpls (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler/bundles/compiler.umd.js:21611:39)
at AngularCompilerProgram.generateFilesForEmit (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler-cli/src/transformers/program.js:736:46)
at AngularCompilerProgram._emitRender2 (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler-cli/src/transformers/program.js:330:27)
at AngularCompilerProgram.emit (/Users/paulo/Projects/eleo-usuario/node_modules/@angular/compiler-cli/src/transformers/program.js:236:22)

Voici quelques code:

package.json

{
    "name": "eleo-usuario",
    "version": "0.2.1",
    "license": "MIT",
    "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e",
        "doc": "./node_modules/.bin/compodoc -p tsconfig.json --serve --port 4100 --theme postmark"
    },
    "private": true,
    "dependencies": {
        "@angular/animations": "^6.1.6",
        "@angular/cli": "^6.1.5",
        "@angular/common": "^6.1.6",
        "@angular/compiler": "^6.1.6",
        "@angular/core": "^6.1.6",
        "@angular/forms": "^6.1.6",
        "@angular/http": "^6.1.6",
        "@angular/platform-browser": "^6.1.6",
        "@angular/platform-browser-dynamic": "^6.1.6",
        "@angular/router": "^6.1.6",
        "@auth0/angular-jwt": "^2.0.0",
        "angular2-jwt": "^0.2.3",
        "angular2-materialize": "^15.0.4",
        "angular2-text-mask": "^9.0.0",
        "chart.js": "^2.7.2",
        "core-js": "^2.5.7",
        "hammerjs": "^2.0.8",
        "jquery": "^3.3.1",
        "materialize-css": "^0.100.2",
        "ng2-charts": "^1.6.0",
        "ng2-file-upload": "^1.3.0",
        "npm": "^6.4.1",
        "rxjs": "^6.3.1",
        "rxjs-compat": "^6.3.1",
        "zone.js": "^0.8.26"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "^0.7.5",
        "@angular/compiler-cli": "^6.1.6",
        "@angular/language-service": "^6.1.6",
        "@types/jasmine": "2.8.8",
        "@types/node": "^10.9.4",
        "codelyzer": "~4.4.4",
        "compodoc": "0.0.41",
        "jasmine-core": "~3.2.1",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "^3.0.0",
        "karma-chrome-launcher": "~2.2.0",
        "karma-cli": "~1.0.1",
        "karma-coverage-istanbul-reporter": "^2.0.2",
        "karma-jasmine": "^1.1.2",
        "karma-jasmine-html-reporter": "^1.3.1",
        "protractor": "^5.4.0",
        "ts-node": "~7.0.1",
        "tslint": "~5.11.0",
        "TypeScript": "^2.9.2"
    }
}

app.component.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { HttpModule, Http, RequestOptions } from '@angular/http';

import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';

import { AppComponent } from './app.component';
import { LoginComponent } from './components/login/login.component';
import { VersoesComponent } from './components/versoes/versoes.component';
import { TutorialComponent } from    './components/tutorial/tutorial.component';

import { FooterPartialComponent } from './partials/footer/footer.partial';
import { SidenavPartialComponent } from './partials/side/sidenav.partial';

import { SharedModule } from './share.module';

import { AuthGuard } from './auth/auth.guard';
import { AuthService } from './services/auth.service';
import { ApiService } from './services/api.service';

import { RootModule } from './components/root/root.module';
import { ClubesModule } from './components/clubes/clubes.module';
import { DistritosModule } from './components/distritos/distritos.module';

import { Router, appRoutingProviders } from './app.routing';

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

@NgModule({
    declarations: [
        AppComponent,
        LoginComponent,
        VersoesComponent,
        TutorialComponent,
        FooterPartialComponent,
        SidenavPartialComponent
    ],
    imports: [
        HttpModule,
        BrowserModule,
        SharedModule,
        RootModule,
        ClubesModule,
        DistritosModule,
        RouterModule.forRoot(Router, { useHash: true })
    ],
    providers: [
        AuthGuard,
        ApiService,
        AuthService,
        appRoutingProviders,
        {
            provide: [AuthHttp, LocationStrategy],
            useClass: HashLocationStrategy,
            useFactory: authHttpServiceFactory,
            deps: [Http, RequestOptions],
        }
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

auth.service.ts (partie principale du code)

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { ApiService } from './api.service';
import { JwtHelper } from 'angular2-jwt';
import 'rxjs/add/operator/filter';
import { Usuario } from '../models/Usuario';

@Injectable()
export class AuthService {

    private jwtHelper: JwtHelper = new JwtHelper();

    constructor(protected router: Router, protected api: ApiService) { }

    public login(model: any): any {
        return new Promise((resolve, reject) => {
            this.api.login(model)
                .subscribe(res => {
                    if (!res.msg) {
                    this.setSession(res);
                }
                resolve(res);
                }, err => console.error(err));
            }
        );
}
2

https://github.com/auth0/angular2-jwt

REMARQUE: cette bibliothèque est maintenant à la version 2 et est publiée sur npm en tant que @ auth0/angular-jwt. Si vous recherchez la version antérieure à la v1.0 de cette bibliothèque, il peut être trouvé dans la branche antérieure à la version 1.0 et sur npm en tant que angular2-jwt. @ auth0/angular-jwt v2 doit être utilisé avec Angular v6 + et RxJS v6 +. Pour Angular v4.3 à v5 +, utilisez @ auth0/angular-jwt v1

vous devez installer @auth0/angular-jwt pour pouvoir utiliser Angular v6 +

7
xeofus