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

feat: new and guard #32

Merged
merged 1 commit into from
Dec 14, 2023
Merged

feat: new and guard #32

merged 1 commit into from
Dec 14, 2023

Conversation

jmcdo29
Copy link
Owner

@jmcdo29 jmcdo29 commented Dec 14, 2023

With the newAndGuard, devs can now set up cases where they want to use an OrGuard to handle an (A && B) || C kind of condition, or any other complex logical approach they'd like. Devs are also now able to correctly pass injection tokens to the AndGuard and OrGuard and use custom providers for their guards that are registered as providers

Copy link

changeset-bot bot commented Dec 14, 2023

🦋 Changeset detected

Latest commit: bc9bbdf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nest-lab/or-guard Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jmcdo29 jmcdo29 force-pushed the feat/and-guard branch 2 times, most recently from 8a6428f to b4735a4 Compare December 14, 2023 19:17
@jmcdo29 jmcdo29 merged commit f1fe0cb into main Dec 14, 2023
5 checks passed
@jmcdo29 jmcdo29 deleted the feat/and-guard branch December 14, 2023 19:23
@its-monotype
Copy link

its-monotype commented Jan 25, 2024

@jmcdo29 Wow, I'm so hyped. I hope I correctly get it that now I can replace this cumbersome solution I had to implement:

import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';

import { JwtAuthGuard } from '@/auth/guards/jwt-auth.guard';
import { RolesGuard } from '@/auth/guards/roles.guard';

@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;
  }
}

with this neat... smth like Or(AndGuard(JwtAuthGuard, RolesGuard), ApiKeyGuard), right? That's what was I looking for!

Thanks for your work, it's super handy! 😃

@jmcdo29
Copy link
Owner Author

jmcdo29 commented Jan 25, 2024

@its-monotype Yes, that's correct. And yes, you still need to add the guards to the providers array so the ModuleRef can grab them dynamically

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

Successfully merging this pull request may close these issues.

2 participants