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

BREAKING CHANGE: update setIssuedAt method to accept Date or milliseconds instead of seconds #102

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# nJwt Change Log

### 3.0.0

* [#102](https://github.com/jwtk/njwt/pull/102) Breaking changes:
- Changes `setIssuedAt` method signature to accept Date or milliseconds as input to be consistent with `setExpiration` and `setNotBefore`
- Excludes `test` folder from the release package


### 2.0.0

* [#98](https://github.com/jwtk/njwt/pull/98) Bumps jsonwebtoken version, drop Node < 12 from engines
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export declare class Jwt {
setJti(jti: string): Jwt;
setSubject(sub: string): Jwt;
setIssuer(iss: string): Jwt;
setIssuedAt(iat: number): Jwt;
setIssuedAt(exp: Date | number | string): Jwt;
setExpiration(exp: Date | number | string): Jwt;
setNotBefore(nbf: Date | number | string): Jwt;
setSigningKey(key: string | Buffer): Jwt;
Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function Jwt(claims, enforceDefaultFields){
}

if (!this.body.iat) {
this.setIssuedAt(nowEpochSeconds());
this.setIssuedAt(new Date().getTime());
}
}

Expand All @@ -180,7 +180,11 @@ Jwt.prototype.setIssuer = function setIssuer(iss) {
return this;
};
Jwt.prototype.setIssuedAt = function setIssuedAt(iat) {
this.body.iat = iat;
if(iat){
this.body.iat = Math.floor((iat instanceof Date ? iat : new Date(iat)).getTime() / 1000);
}else{
delete this.body.iat;
}
return this;
};
Jwt.prototype.setExpiration = function setExpiration(exp) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"name": "njwt",
"version": "2.0.0",
"version": "3.0.0",
"description": "JWT Library for Node.js",
"engines": {
"node": ">=12.0"
},
"files": ["index.js","index.d.ts","properties.json","CHANGELOG.md","README.md","test"],
"files": [
"index.js",
"index.d.ts",
"properties.json",
"CHANGELOG.md",
"README.md"
],
"main": "index.js",
"types": "index.d.ts",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions test/types/jwt.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const jwt = new Jwt({}, true);
expectType<string>(jwt.sign({}, 'alg', 'signingKeyValue'));
expectType<Jwt>(jwt.setHeader('headerParam', 'headerValue'));
expectType<Jwt>(jwt.setIssuedAt(123456));
expectType<Jwt>(jwt.setIssuedAt(new Date()));
expectType<Jwt>(jwt.setIssuer('atko'));
expectType<Jwt>(jwt.setJti('uuid'));
expectType<Jwt>(jwt.setNotBefore(new Date()));
Expand Down
Loading