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

type error while using expiresIn in signAsync #1369

Closed
2 of 4 tasks
aayushchugh opened this issue Jul 15, 2023 · 7 comments
Closed
2 of 4 tasks

type error while using expiresIn in signAsync #1369

aayushchugh opened this issue Jul 15, 2023 · 7 comments
Labels
bug Something isn't working needs triage

Comments

@aayushchugh
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am not able to use expiresIn option while signing a token

Getting error

No overload matches this call.
  Overload 1 of 2, '(payload: string, options?: Omit<JwtSignOptions, keyof SignOptions>): Promise<string>', gave the following error.
    Argument of type '{ secret: string; expiresIn: string; }' is not assignable to parameter of type 'Omit<JwtSignOptions, keyof SignOptions>'.
      Object literal may only specify known properties, and 'expiresIn' does not exist in type 'Omit<JwtSignOptions, keyof SignOptions>'.
  Overload 2 of 2, '(payload: object | Buffer, options?: JwtSignOptions): Promise<string>', gave the following error.
    Argument of type 'string' is not assignable to parameter of type 'object | Buffer'.ts(2769)

In following code

const accessToken = this.jwtService.signAsync(String(user.id), {
     secret: accessTokenPublicKey,
     expiresIn: "15m",
});

Code for module

@Module({
	controllers: [AuthController],
	providers: [AuthService],
	imports: [JwtModule.register({ global: true, signOptions: { algorithm: "RS256" } })],
})
export class AuthModule {}

I can use expiresIn in JwtModule.register() but now while using it in service

Minimum reproduction code

https://github.com/maya-manager/server/blob/aayushchugh/login/src/modules/auth/auth.service.ts#L164-L167

Steps to reproduce

No response

Expected behavior

I should be able to use expiresIn without any type errors

Package version

10.1.0

NestJS version

10.0.5

Node.js version

18.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

image

Also, VScode is only showing autocomplete for these 2 options

image

Here there are two signAsync and in the upper one you are using Omit. I think Omit is removing all the types from SignOptions and I can't use expiresIn

image
@aayushchugh aayushchugh added bug Something isn't working needs triage labels Jul 15, 2023
@aayushchugh
Copy link
Author

aayushchugh commented Jul 15, 2023

After downgrading the package to 10.0.3 the problem was solved and I was able to access expiresIn

and in this version, there are no duplicate signAsync. Also, there is no Omit

image

I can solve this issue in a PR if you assign me this issue

@kamilmysliwiec
Copy link
Member

PRs are more than welcome 🙏

@aayushchugh
Copy link
Author

@kamilmysliwiec Assign this to me please

aayushchugh added a commit to aayushchugh/nestjs-jwt that referenced this issue Jul 17, 2023
@Hender-hs
Copy link

Hender-hs commented Aug 25, 2023

Does your code run in 10.0.3 version? Test it.
It must break.

JsonWebToken doesn't allow you use payload as string and set a "invalid" options, such as: expiresIn, notBefore, etc.
Except: privateKey and secret.

See /jsonwebtoken/sign.js

@aayushchugh
Copy link
Author

@Hender-hs My code was working in 10.0.3 without any problem

@tianangthang7
Copy link

why don't we fix that

@kamilmysliwiec
Copy link
Member

#1370

@nestjs nestjs locked and limited conversation to collaborators Sep 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants