Skip to content

Commit cd702d5

Browse files
authored
Merge pull request #54 from diskcloud/feature/51
fix(auth): #51 JWT 及 Redis 改为通用环境变量'TOKEN_EXPIRE_TIME'
2 parents 5e105c4 + 32c03cb commit cd702d5

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

routers/users.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
USERS_LOGIN_POST,
1212
USER_REST_PARAMS_PATCH,
1313
} = require("../types/schema/users");
14+
const { convertTimeFormat } = require("../utils/convertTimeFormat")
1415

1516
const { validateBody, validateParams } = require("../types");
1617
const { USER_STATUS, USER_ACTION_TYPES } = require("../constants/users");
@@ -41,7 +42,7 @@ router.post("/sessions", validateBody(USERS_LOGIN_POST), async (ctx) => {
4142
const token = jwt.sign(
4243
{ id: user.id, username: user.username },
4344
process.env.JWT_SECRET,
44-
{ expiresIn: process.env.JWT_EXPIRES_IN }
45+
{ expiresIn: convertTimeFormat(process.env.TOKEN_EXPIRE_TIME, 's') }
4546
);
4647

4748
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
@@ -52,7 +53,7 @@ router.post("/sessions", validateBody(USERS_LOGIN_POST), async (ctx) => {
5253

5354
// 将 token 存储在 Redis 中
5455
await redisClient.set(`user_login:${user.id}`, token, {
55-
EX: process.env.USER_LOGIN_TOKEN_EXPIRE_TIME,
56+
EX: process.env.TOKEN_EXPIRE_TIME,
5657
});
5758

5859
user.update({

scripts/setup.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ if (-Not (Test-Path .\.env.local)) {
3232
$MYSQL_PORT = Prompt-Input "Enter MySQL Port" "3306"
3333
$REDIS_HOST = Prompt-Input "Enter Redis Host" "127.0.0.1"
3434
$REDIS_PORT = Prompt-Input "Enter Redis Port" "6379"
35-
$USER_LOGIN_TOKEN_EXPIRE_TIME = Prompt-Input "Enter User Login Token Expire Time" "3600"
35+
$TOKEN_EXPIRE_TIME = Prompt-Input "Enter User Login Token Expire Time" "86400(1d)"
3636
$JWT_EXPIRES_IN = Prompt-Input "Enter JWT Expiry Time" "1h"
3737
$JWT_SECRET = Prompt-Input "Enter JWT Secret"
3838

scripts/setup.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ if [ ! -f .env.local ]; then
3232
MYSQL_PORT=$(prompt "MYSQL_PORT" "Enter MySQL Port" "3306")
3333
REDIS_HOST=$(prompt "REDIS_HOST" "Enter Redis Host" "127.0.0.1")
3434
REDIS_PORT=$(prompt "REDIS_PORT" "Enter Redis Port" "6379")
35-
USER_LOGIN_TOKEN_EXPIRE_TIME=$(prompt "USER_LOGIN_TOKEN_EXPIRE_TIME" "Enter User Login Token Expire Time" "3600")
36-
JWT_EXPIRES_IN=$(prompt "JWT_EXPIRES_IN" "Enter JWT Expiry Time" "1h")
35+
TOKEN_EXPIRE_TIME=$(prompt "TOKEN_EXPIRE_TIME" "Enter User Login Token Expire Time" "86400(1d)")
3736
JWT_SECRET=$(prompt "JWT_SECRET" "Enter JWT Secret")
3837

3938
# Create .env.local file

utils/convertTimeFormat.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function convertTimeFormat(time, unit = "ms") {
2+
const timeUnits = [
3+
{ unit: "y", ms: 365 * 24 * 60 * 60 * 1000 }, // 年
4+
{ unit: "M", ms: 30 * 24 * 60 * 60 * 1000 }, // 月
5+
{ unit: "w", ms: 7 * 24 * 60 * 60 * 1000 }, // 周
6+
{ unit: "d", ms: 24 * 60 * 60 * 1000 }, // 天
7+
{ unit: "h", ms: 60 * 60 * 1000 }, // 小时
8+
{ unit: "m", ms: 60 * 1000 }, // 分钟
9+
{ unit: "s", ms: 1000 }, // 秒
10+
];
11+
12+
if (typeof time === "string") {
13+
time = parseInt(time, 10);
14+
if (isNaN(time)) {
15+
throw new Error("Invalid input: unable to convert string to number.");
16+
}
17+
} else if (typeof time !== "number") {
18+
throw new Error(
19+
"Invalid input type: input should be a number or a numeric string."
20+
);
21+
}
22+
23+
// 将秒转换为毫秒
24+
if (unit === "s") {
25+
time = time * 1000;
26+
}
27+
28+
for (let i = 0; i < timeUnits.length; i++) {
29+
const { unit, ms } = timeUnits[i];
30+
if (time >= ms) {
31+
const value = Math.floor(time / ms);
32+
return `${value}${unit}`;
33+
}
34+
}
35+
36+
return `${time}ms`; // For cases less than 1 second
37+
}
38+
39+
module.exports = {
40+
convertTimeFormat,
41+
};

0 commit comments

Comments
 (0)