You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.tsimport{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],})exportclassAppModule{publicconfigure(consumer: MiddlewareConsumer){consumer.apply(AuthenticationMiddleware).exclude({path: '/login',method: RequestMethod.ALL},).forRoutes({path: '/*',method: RequestMethod.ALL})}}// authentication.middleware.tsimport{Injectable,NestMiddleware}from'@nestjs/common';import{Request,Response}from'express';
@Injectable()exportclassAuthenticationMiddlewareimplementsNestMiddleware{// just testing out right here, thats why i just use a query parameteruse(req: Request,res: Response,next: ()=>void){if(req.query.loggedin!=='true'){constmessage='Sorry, we were unable to process your request.';returnres.send(message);}next();}}// default.controller.tsimport{Controller,Get,Post,Body,UsePipes,UseGuards,Req,Res}from'@nestjs/common';import{Request,Response}from'express';
@Controller('/')exportclassDefaultController{
@Get()asyncgetDashboard(): Promise<string>{console.log(`get dashboard`);return'dashboard';}}// login.controller.tsimport{Controller,Get,Post,Body,UsePipes,UseGuards,Req,Res}from'@nestjs/common';import{Request,Response}from'express';
@Controller('/login')exportclassLoginController{
@Get()asyncgetLogin(): Promise<string>{console.log(`getlogin`);return'get login page';}
@Post()asynclogin(@Res()response: Response): Promise<string>{console.log(`post login`);return'loggin in';}}
[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
The text was updated successfully, but these errors were encountered:
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
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
The text was updated successfully, but these errors were encountered: