Skip to content

Commit f49a67f

Browse files
committed
refactor(jwt generation): Simplify payload property assignments and secret initialization
1 parent c3986d7 commit f49a67f

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/server/src/templates/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,23 @@ export function generateJwt(options: GenerateJWTOptions = {}): string {
8585
alg: "HS256",
8686
typ: "JWT",
8787
});
88-
payload.iss || (payload.iss = "dokploy");
89-
payload.iat || (payload.iat = Math.floor(Date.now() / 1000));
90-
payload.exp ||
91-
(payload.exp = Math.floor(
92-
new Date("2030-01-01T00:00:00Z").getTime() / 1000,
93-
));
88+
if (!payload.iss) {
89+
payload.iss = "dokploy";
90+
}
91+
if (!payload.iat) {
92+
payload.iat = Math.floor(Date.now() / 1000);
93+
}
94+
if (!payload.exp) {
95+
payload.exp = Math.floor(new Date("2030-01-01T00:00:00Z").getTime() / 1000);
96+
}
9497
const encodedPayload = objToJWTBase64({
9598
iat: Math.floor(Date.now() / 1000),
9699
exp: Math.floor(new Date("2030-01-01T00:00:00Z").getTime() / 1000),
97100
...payload,
98101
});
99-
secret || (secret = randomBytes(32).toString("hex"));
102+
if (!secret) {
103+
secret = randomBytes(32).toString("hex");
104+
}
100105
const signature = safeBase64(
101106
createHmac("SHA256", secret)
102107
.update(`${encodedHeader}.${encodedPayload}`)

0 commit comments

Comments
 (0)