ssilvert / keycloak-schematic

Schematic to add Keycloak support Angular CLI applications
Apache License 2.0
31 stars 5 forks source link

Error constructing library with keycloak #7

Open rafaelvicio opened 6 years ago

rafaelvicio commented 6 years ago

I'm creating a simple library that will use the keycloak, but I'm getting the following error:

/home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1: Error encountered in metadata generated for exported symbol 'KeycloakService': /home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12: Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler. {"symbolic":"error","message":"Variable not initialized","line":17,"character":11} Error: /home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:13:1: Error encountered in metadata generated for exported symbol 'KeycloakService': /home/rafaelvicio/desenv/my-lib/arquitetura-web/projects/pf-siseg/src/lib/keycloak-service/keycloak.service.ts:18:12: Metadata collected contains an error that will be reported at runtime: Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler. {"symbolic":"error","message":"Variable not initialized","line":17,"character":11} at /home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/metadata/collector.js:707:31 at Array.forEach () at validateMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/metadata/collector.js:695:46) at MetadataCollector.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/metadata/collector.js:550:21) at MetadataCache.getMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_cache.js:86:41) at Object.getSourceFileMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:112:56) at Object.readMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/metadata_reader.js:46:37) at TsCompilerAotCompilerTypeCheckHostAdapter.getMetadataFor (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler-cli/src/transformers/compiler_host.js:464:38) at StaticSymbolResolver.getModuleMetadata (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:480:49) at StaticSymbolResolver._createSymbolsOf (/home/rafaelvicio/desenv/my-lib/arquitetura-web/node_modules/@angular/compiler/src/aot/static_symbol_resolver.js:268:33)

keycloak.service.ts:

import { Injectable } from '@angular/core'; import { KeycloakLoginOptions } from './keycloak.d';

import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs/Observable';

import * as Keycloak from './keycloak';

export type KeycloakClient = Keycloak.KeycloakInstance; type InitOptions = Keycloak.KeycloakInitOptions;

@Injectable() export class KeycloakService {

constructor(private http: HttpClient) { }

static keycloakAuth: KeycloakClient;

/**
 * Configure and initialize the Keycloak adapter.
 *
 * @param configOptions Optionally, a path to keycloak.json, or an object containing
 *                      url, realm, and clientId.
 * @param adapterOptions Optional initiaization options.  See javascript adapter docs
 *                       for details.
 * @returns {Promise<T>}
 */
static init(configOptions?: string | {}, initOptions?: InitOptions): Promise<any> {
    KeycloakService.keycloakAuth = Keycloak(configOptions);

    return new Promise((resolve, reject) => {
        KeycloakService.keycloakAuth.init(initOptions)
            .success(() => {
                resolve();
            })
            .error((errorData: any) => {
                reject(errorData);
            });
    });
}

/**
 * Expose the underlying Keycloak javascript adapter.
 */
client(): KeycloakClient {
    return KeycloakService.keycloakAuth;
}

authenticated(): boolean {
    return KeycloakService.keycloakAuth.authenticated;
}

login(options?: KeycloakLoginOptions) {
    KeycloakService.keycloakAuth.login(options);
}

logout(redirectUri?: string) {
    KeycloakService.keycloakAuth.logout({ redirectUri: redirectUri });
}

account() {
    KeycloakService.keycloakAuth.accountManagement();
}

authServerUrl(): string {
    return KeycloakService.keycloakAuth.authServerUrl;
}

realm(): string {
    return KeycloakService.keycloakAuth.realm;
}

getToken(): Promise<string> {
    return new Promise<string>((resolve, reject) => {
        if (KeycloakService.keycloakAuth.token) {
            KeycloakService.keycloakAuth
                .updateToken(5)
                .success(() => {
                    resolve(<string>KeycloakService.keycloakAuth.token);
                })
                .error(() => {
                    reject('Failed to refresh token');
                });
        } else {
            reject('Not loggen in');
        }
    });
}

}