Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not inject dependency in my CustomAuthProvider #380

Open
khanorder opened this issue May 16, 2022 · 1 comment
Open

Can not inject dependency in my CustomAuthProvider #380

khanorder opened this issue May 16, 2022 · 1 comment

Comments

@khanorder
Copy link

khanorder commented May 16, 2022

i'm trying initialize AuthProvider as described on readme.

but i can not inject dependency in my CustomAuthProvider

If inject without constructor as described on readme, All injected dependencies are undefined.

@injectable()
export class CustomAuthProvider implements interfaces.AuthProvider {

    @inject(LoggerService.name) private readonly _loggerService!: LoggerService;
    @inject(DBService.name) private readonly _db!: DBService;
    @injectHttpContext private readonly _httpContext!: HttpContext;

    public async getUser(
        req: express.Request,
        res: express.Response,
        next: express.NextFunction
    ): Promise<interfaces.Principal> {
        let user: Auth.User|null = null;
        try {
            user = await this._db.SelectUserInfo(req.session.user.email);
            // this._db is undefined
        } catch (error) {
            this._loggerService.logger.error(error);
            // this._loggerService is undefined too.
        }
        const principal = new Principal(user);
        return principal;
    }

}

And if inject a dependency to CustomAuthProvider on constructor of CustomAuthProvider, error occured on line of initializing InversifyExpressServer.

//customAuthProvider.ts

@injectable()
export class CustomAuthProvider implements interfaces.AuthProvider {

    constructor (
        @inject(LoggerService.name) private readonly _loggerService: LoggerService,
        @inject(DBService.name) private readonly _db: DBService,
        @injectHttpContext private readonly _httpContext: HttpContext
    ) {
    }

    public async getUser(
        req: express.Request,
        res: express.Response,
        next: express.NextFunction
    ): Promise<interfaces.Principal> {
        let user: Auth.User|null = null;
        try {
            user = await this._db.SelectUserInfo(req.session.user.email);
        } catch (error) {
            this._loggerService.logger.error(error);
        }
        const principal = new Principal(user);
        return principal;
    }

}

// app.ts
this.container = new Container();
this.container.bind(LoggerService.name).to(LoggerService);
this.container.bind(DBService.name).to(DBService).inSingletonScope();
this.container.bind(CustomAuthProvider.name).to(CustomAuthProvider).inSingletonScope();

this.server = new InversifyExpressServer(this.container, null, null, null, CustomAuthProvider); // error is occured on this line.
//Argument of type 'typeof CustomAuthProvider' is not assignable to parameter of type 'new () => AuthProvider'.
//Types of construct signatures are incompatible.
//Type 'new (_loggerService: LoggerService, _db: DBService, _httpContext: HttpContext) => CustomAuthProvider' is not assignable to type 'new () => AuthProvider'.ts(2345)

How can i use another dependencies on CustomAuthProvider?

@Seth-Carter
Copy link

Seth-Carter commented May 24, 2022

You can't inject in the constructor because the interface.AuthProvider doesn't allow it.

How you did it in your first example is right, although you don't need the httpContext, since the getUser method receives req, res, and next already.

I suspect there's something wrong with your container configuration. Your logger is most likely a simple class instance without any of its dependencies and should be bound with toConstantValue().

Also, you should use generics with bind methods:
this.container.bind<DBService>(DBService.name).to(DBService).inSingletonScope()

If you don't, Inversify will struggle to resolve your bindings. At least I've had issues in the past when I've tried to leave out the generic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants