Nest: Access express from ApplicationModule or inject own service into ApplicationModule

Created on 12 Jul 2017  路  9Comments  路  Source: nestjs/nest

Hello everyone,

I am new to this Framework and got the following problem:

I am using TypeORM to connect to the database which is running as a service and gets injected into all components/controllers where it is needed - everything is running fine.

Now I tried to implement passport with a local strategy: I want to check the userdetails from my database using the TypeORM service I've already created - and that's where my problem starts: I want to use one database connection in the whole application, as I can't instantiate the TypeORM connection pool more than once (https://github.com/typeorm/typeorm/issues/592).

Is there a best practice for those kind of case? Can I instantiate my TypeORM Service outside the main ApplicationModule and then inject it into it in order to use it in both, passport and the app itself?

Or is it possible to access the express instance from within the ApplicationModule in order to make passport use the TypeORM service instantiated by nestjs?

Any help would be very appreciated.

question 馃檶

All 9 comments

@makislav your problem will be solved if @kamilmysliwiec will accept my PR #100 , then you will be able to do what you want.

Hey @cojack, I've already taken a look at it, there's still the question how I can combine the created TypeORM service with passport / the instance of express.

Example

import "reflect-metadata";

import * as express from 'express';
import * as compression from 'compression';
import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as expressSession from 'express-session';
import * as passport from "passport";
import * as connectMongo from "connect-mongo";

import {NestFactory} from '@nestjs/core';
import {ApplicationModule} from './app/app.module';
import {Environment} from "./app/shared/config/config";
import {PassportService} from "./app/shared/services/passport/passport.service";

//
new PassportService(passport);

// Middleware
const instance = express();
instance.use(compression());
instance.use(bodyParser.json());
instance.use(cookieParser());
instance.use(expressSession({
    secret: Environment.session.secret,
    resave: false,
    saveUninitialized: false
}));
instance.use(passport.initialize());
instance.use(passport.session());

const app = NestFactory.create(ApplicationModule, instance);
app.listen(3000, () => console.log('Application is listening on port 3000.'));

This is my current initialization file. I am using PassportService (similar to the example in the docs) to tell passport what it has to do (i.e. query the database). So even if your PR makes it possible to wait for a successful initialization of services etc inside the app, there's still the question how I can tell my PassportService (which is outside of the app) how to use the TypeORM Connection which is inside the app.

(Or how to tell passport/express what to do from inside of the ApplicationModule)

@makislav Im sorry but I was wrong about your situation. This I guess should be modified to be used inside of nest. I did not implement passport yet but it looks like it might work when you will pass the PassportService to your AppModule and try to initialize it in constructor or something like this.

Hey @makislav,
You only have to move your passport strategy class inside Nest, something like this:

@Component()
export class JwtStrategy extends Strategy {
   constructor(
        @Inject(JWTConfigToken) config: JWTConfig,
        private readonly authService: AuthService) {

        super({
            jwtFromRequest: ExtractJwt.fromAuthHeader(),
            passReqToCallback: true,
            secretOrKey: config.secretOrKey,
        }, async (req, payload, next) => await this.verify(req, payload, next));

        passport.use(this);
    }
    /*....*/
}

Hey guys. Is there anywhere a full example of how to implement passpord jwt authorisation? I'd appreciate to have this

Hey ! You can see my repo you will find what you search

@adrien2p I see that. Good example. Thank you!

@a-pvlov-parc my pleasure

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breitsmiley picture breitsmiley  路  3Comments

cojack picture cojack  路  3Comments

anyx picture anyx  路  3Comments

mishelashala picture mishelashala  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments