ferrerojosh / nest-keycloak-connect

keycloak-nodejs-connect module for Nest
MIT License
318 stars 123 forks source link

Cannot inject KeycloakMultiTenantService #173

Closed mhadi512 closed 10 months ago

mhadi512 commented 10 months ago

I have a custom service to do manual token validation to handle requests coming from Websockets gateway. But I have implemented multi-tenancy recently, so this service needs to be updated to use the multi-tenancy options and resolvers.

here is part of the service I'm trying to inject the KeycloakMultiTenantService into:

import KeycloakConnect, { Grant, Token } from 'keycloak-connect';
import {
    KEYCLOAK_CONNECT_OPTIONS,
    KEYCLOAK_MULTITENANT_SERVICE,
    KeycloakConnectConfig,
    KeycloakMultiTenantService,
    TokenValidation,
} from 'nest-keycloak-connect';

@Injectable()
export class KeycloakConnectService {
    private logger: Logger = new Logger(KeycloakConnectService.name);

    constructor(
        private cls: AppClsService,
        @Inject(KEYCLOAK_CONNECT_OPTIONS)
        public keycloakOpts: KeycloakConnectConfig,
        @Inject(KEYCLOAK_MULTITENANT_SERVICE)
        public keycloakMultitenantService: KeycloakMultiTenantService,
    ) {}

    private getKeycloakInstance(realm: string) {
        return this.keycloakMultitenantService.get(realm);
    }
}

AdminstrationModule (the same module is importing the Keycloak connect module and my custom service):

KeycloakConnectModule.registerAsync({
    inject: [KeycloakAdminService, AppClsService],
    imports: [AdministrationModule],
    useFactory: async (kcAdmin: KeycloakAdminService, cls: AppClsService) => {
        return {
            authServerUrl: getEnv('KC_URL'),
            clientId: CLIENTS.BE,
            tokenValidation: TokenValidation.OFFLINE,
            secret: '',
            multiTenant: {
                resolveAlways: true,
                realmResolver: () => {
                    return cls.get('TENANT_KEY');
                },
                realmSecretResolver: realm => kcAdmin.realmSecretResolver(realm),
            },
        };
    },
})

But I get the below error: Nest can't resolve dependencies of the KeycloakConnectService (AppClsService, KEYCLOAK_CONNECT_OPTIONS, ?). Please make sure that the argument KEYCLOAK_MULTITENANT_SERVICE at index [2] is available in the AdministrationModule context.

First I was using an old version of this package, but I noticed that there is a new version (1.9.4) which exports the KeycloakMultiTenantService. but still getting the same error.

What am I missing?

Thanks for this great NestJS wrapper.

mhadi512 commented 9 months ago

I spent time checking my app imports, and I was about to create a new fresh Nest app to check if it's an issue in the package or my code. Closing this issue without any reference to a fix or a PR is not nice.

I'm very thankful for this awesome package, really appreciated. But it would be great if you reply with a comment saying that this issue has been fixed in 1.9.5 before closing it.