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

consumer.exclude() does not exclude the routes from middleware check #3208

Closed
JustAMicrobe opened this issue Oct 17, 2019 · 2 comments
Closed
Labels
needs triage This issue has not been looked into

Comments

@JustAMicrobe
Copy link

Bug Report

Current behavior

I want to create an app in which an authentication middleware is called for all routes except for the '/login' path. I used consumer.apply(AuthMiddleware).exclude() function for that, but it does not seem to work. i also found in issue 790 that i might have to use the with() function, but i dont find some examples for it in the documentation.

currently the middleware always catches the request and responds with the error message, no matter what route i try to access. even those that are registered in the .exclude() function. But when I access a url adding the "?loggedin=true" parameter, i will pass the middleware and proceed to get the responses defined in the respective route.
i would expect to pass the middleware for all requests to the /login endpoint, no matter if i provide the loggedin=true parameter in the query or not.

Remark: i just use the query parameter for simplicity and testing.

Input Code

// app.module.ts
import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { AuthenticationMiddleware } from './common/authentication.middleware';

import { LoginController } from './login/login.controller';
import { LoginService } from './login/login.service';
import { DefaultController } from './default/default.controller';

@Module({
  imports: [],
  controllers: [LoginController, DefaultController],
  providers: [LoginService],
})
export class AppModule {
  public configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(AuthenticationMiddleware)
      .exclude( 
        { path: '/login', method: RequestMethod.ALL },
      )
      .forRoutes({path: '/*', method: RequestMethod.ALL})
  }
}

// authentication.middleware.ts
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from 'express';

@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {
  // just testing out right here, thats why i just use a query parameter
  use(req: Request, res: Response, next: () => void) {
    if (req.query.loggedin !== 'true') {
      const message = 'Sorry, we were unable to process your request.';
      return res.send(message);
    }
    next();
  }
}

// default.controller.ts
import { Controller, Get, Post, Body, UsePipes, UseGuards, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';

@Controller('/')
export class DefaultController {
  @Get()
  async getDashboard(): Promise<string> {
    console.log(`get dashboard`);
    return 'dashboard';
  }
}

// login.controller.ts
import { Controller, Get, Post, Body, UsePipes, UseGuards, Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';

@Controller('/login')
export class LoginController {
  @Get()
  async getLogin(): Promise<string> {
    console.log(`getlogin`);
    return 'get login page';
  }

  @Post()
  async login(@Res() response: Response): Promise<string> {
    console.log(`post login`);
    return 'loggin in';
  }
}

Expected behavior

When i try to access the url GET http://localhost:3000/login i should get "get login page" as a response.
When i try to access GET http://localhost:3000 i should get "Sorry, we were unable to process your request." as a response.
When i try to acces GET http://localhost:3000?loggedin=true i should get "dashboard" as a response.

Possible Solution

Environment


[System Information]
OS Version     : Windows 10
NodeJS Version : v10.16.0
NPM Version    : 6.9.0
[Nest Information]
platform-express version    : 6.7.2
common version              : 6.7.2
core version                : 6.7.2

@JustAMicrobe JustAMicrobe added the needs triage This issue has not been looked into label Oct 17, 2019
@kamilmysliwiec
Copy link
Member

See this #3042

@lock
Copy link

lock bot commented Jan 24, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants