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: Add getRequest method int IAuthGuard interface #502

Merged
merged 2 commits into from
Jun 13, 2023

Conversation

ferjul17
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: N/A

Currently I have to override the type returned by AuthGuard() otherwise typescript complains because super.getRequest() does not exist.

export class JwtAuthGuard extends (AuthGuard('jwt') as {
  new (...args: any[]): IAuthGuard & { getRequest(context: ExecutionContext): Request };
}) {
  public getRequest(context: ExecutionContext): Request {
    return super.getRequest(context) ?? GqlExecutionContext.create(context).getContext().req;
  }
}

What is the new behavior?

With these changes, I do not need this "hack" anymore, the type returned by AuthGuard() contains the method getRequest().

export default class JwtAuthGuard extends AuthGuard('jwt') {
  public getRequest(context: ExecutionContext): Request {
    return super.getRequest(context) ?? GqlExecutionContext.create(context).getContext().req;
  }
}

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@ferjul17 ferjul17 force-pushed the feat/getRequest-in-IAuthGuard branch from d2ac367 to d6e5d2d Compare February 10, 2021 15:02
if (err || !user) {
throw err || new UnauthorizedException();
}
return user;
return user as any as TUser;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please revert back changes unrelated to this PR (move the type argument, generic parameter)? Otherwise, it looks good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your feedback. I did that to be able to compile. I got an error without this change. But I'll try to do something better :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be good now :)

@ferjul17 ferjul17 force-pushed the feat/getRequest-in-IAuthGuard branch from d6e5d2d to 871bf00 Compare February 16, 2021 08:05
};
export const AuthGuard: (
type?: string | string[]
) => Type<IAuthGuard> = memoize(createAuthGuard);

const NO_STRATEGY_ERROR = `In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.`;

function createAuthGuard(type?: string | string[]): Type<CanActivate> {
function createAuthGuard(type?: string | string[]): Type<IAuthGuard> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really necessary? I think adding the type directly in the IAuthGuard type definition is enough.

@RodolfoSilva
Copy link

Good job @ferjul17, I was about to send a similar PR.

@kamilmysliwiec have any plans to include this in the next release?

@kamilmysliwiec kamilmysliwiec changed the base branch from master to 10.0.0 June 13, 2023 07:38
@kamilmysliwiec kamilmysliwiec merged commit 767234a into nestjs:10.0.0 Jun 13, 2023
1 check failed
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.

None yet

3 participants