Closed its-monotype closed 9 months ago
Btw this custom solution works fine:
@Injectable()
export class JwtAuthRolesCompositeGuard implements CanActivate {
constructor(
private jwtAuthGuard: JwtAuthGuard,
private rolesGuard: RolesGuard,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const hasJwtAuthAccess = await this.jwtAuthGuard.canActivate(context);
const hasRolesAccess = await this.rolesGuard.canActivate(context);
if (hasJwtAuthAccess && hasRolesAccess) {
return true;
}
return false;
}
}
Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).
@jmcdo29 Here you go https://github.com/its-monotype/nest-and-guard
curl -X POST http://localhost:3000/admin/and-guard -d '{"username": "john", "password": "changeme"}' -H "Content-Type:
application/json"
{"message":"Forbidden resource","error":"Forbidden","statusCode":403}
If you will try to do the same but with endpoint that not uses AndGuard
it works fine, try running:
curl -X POST http://localhost:3000/admin -d '{"username": "john", "password": "changeme"}' -H "Content-Type: application/json"
Please, take a look at app.controller.ts
Ah, okay, yep I see. So the AndGuard
, at the moment, is designed to not care about the order of execution of the guards it's told about. That's to make it as quick as possible. However, I can certainly see situations where you want it to be order dependent. Maybe I can add a new option that dictates the guards should be ran sequentially instead of in parallel
Should be available in @nest-lab/or-guard@2.4.0
Should be available in
@nest-lab/or-guard@2.4.0
Wow, that was fast! I'll try it soon and let you know how it goes. Thanks for considering my request! 😃
Works perfectly, thanks again
I have a problem when using
AndGuard
withJwtAuthGuard
from passport.js andRolesGuard
.In
JwtAuthGuard
user fetched in DB (inside jwt passport strategy) and returned, this will attach it to the request.and inside
RolesGuard
I'm extracting the user fromcontext.switchToHttp().getRequest()
but I receive
undefined
when usingAndGuard
, btw without it everything works fine, I assume thatJwtAuthGuard
must be executed beforeRolesGuard
so it will attach the user to req before so RolesGuard can grab it